Tag Archives: time

PHP: How to Add Days, Months or Years to a Date Programming 14 MAY 2012

Date work is always icky, but luckily PHP has some pretty nifty functions that can pretty much do all the heavy lifting for you.

One of most commonly faced problems is how to add either a number of days, months or years to a given date, and thanks to PHP’s fantastic strtotime function and its natural language (well sort of) parsing, this becomes pretty trivial to solve.

Taken from the PHP manual: The strtotime function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

To see it in action against the current date, try running:

echo '<p>' . date('Y-m-d', strtotime('+1 day')) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+2 weeks')) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+3 months')) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+4 years')) . '</p>';

Of course, if you want to use a different base for its calculations, you would use the function like so:

echo '<p>' . date('Y-m-d', strtotime('+1 day', strtotime('2000-12-31'))) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+2 weeks', strtotime('2000-12-31'))) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+3 months', strtotime('2000-12-31'))) . '</p>';
echo '<p>' . date('Y-m-d', strtotime('+4 years', strtotime('2000-12-31'))) . '</p>';

Nifty.

Related Link: http://php.net/manual/en/function.strtotime.php

Ensure that an Ubuntu PC’s Time Stays Correct and Up-to-date CodeUnit 12 OCT 2011

Some older machines suffer from a system clock that systematically loses time, resulting in your operating system, in this case Ubuntu, displaying the incorrect time and in the process causing you to be late for all you carefully scheduled appointments, unless of course you have a second time source on hand.

One way to avoid this is to force Ubuntu to keep its clock synchronized with Internet servers, meaning a quick check on boot up, and a time that should always be pretty accurate, unless of course you don’t have network connection and your system clock failed miserable at the last power down sequence.

To install NTP, simply open up a terminal window and execute:

sudo apt-get install ntp

Reboot your machine and voila, your clock should now be reflecting an accurate time for your selected time zone! :)

JavaScript: Calculate the Number of Days in a Month CodeUnit 02 SEP 2011

A simple way to calculate how many days are in a month in Javascript is to leverage Javascript’s built in date overflow feature – basically if you give it an incorrect date it automatically adjusts it to the correct one by assuming that the overflow are days which need simply to be added to the given date.

So, given a month and a year, it is a simply matter of forcing a date to overflow by a certain amount and then by subtracting the result from that overflow amount to learn how many days are in that month.

For example, to calculate the number of days in the current month:

var time=new Date();
var numberOfDays = 32 - new Date(time.getFullYear (), time.getMonth(), 32).getDate();

Nifty.

JavaScript: Calculate the Number of Days in Last Month and Set a Date Range for it CodeUnit 01 JUL 2011

A simple way to calculate how many days are in a month in Javascript is to leverage Javascript’s built in date overflow feature – basically if you give it an incorrect date it automatically adjusts it to the correct one by assuming that the overflow are days which need simply to be added to the given date.

So, given a month and a year, it is a simply matter of forcing a date to overflow by a certain amount and then by subtracting the result from that overflow amount to learn how many days are in that month.

Thus by setting the start marker to one month back in time, we can easily learn how many days were in the last month, making it a snap to provide a quick last month range pick for a calendar control.

var time=new Date();
//force the date object to last month
var currentMonth = time.getMonth();
time.setMonth(time.getMonth()-1);
while(currentMonth == time.getMonth()){
    time.setMonth(time.getMonth()-1);
}
//get the last day of last month
var lastDay = 32 - new Date(time.getFullYear (), time.getMonth(), 32).getDate();
//if we were setting a last month range for a calendar control
var monthstart = new Date(time.getFullYear (),time.getMonth(),1);
var monthend = new Date(time.getFullYear (),time.getMonth(),lastDay);

Nifty.

Ubuntu: How to View the Current Date and Time via Command Line CodeUnit 14 JUN 2010

If you are working on an Ubuntu server installation that comes in the command line only variation, you might at any point in time want to see just what exactly the server’s time is currently set to.

Needless to say, getting the current date and time information is pretty damn easy – simply call:

date

Seriously, as simple as that.

Now if you wanted to say change the date or time on the server, it turns out that this is pretty simple to acheive as well:

Simply provide a parameter to the date function!

So we would then have:

sudo date newdatetimestring

Of course, you won’t exactly use “newdatetimestring”, rather it stands for the date time format “nnddhhmmyyy.ss” where nn is a two digit month (01 to 12), dd is a two digit day (01 to 31), hh is two digit hour (00 to 23), yyyy a four digit year and finally .ss which is two digit seconds (00 to 59) [notice the ‘period’ there by the way].

Nice!

PHP: How to add a Hour to the Current Time CodeUnit 23 FEB 2010

Adding an hour to the current time turns out to be pretty easy thanks to the fantastic strtotime PHP function.

For example, should we wish to grab the hour that follows on from the current hour, we can simply make use of the code sample at the bottom of this entry.

This code snippet will echo out the following hour in 24-hour format, meaning that if the time was say currently 09:47, the script would echo out 10:

echo date('H',(strtotime("+1 hour"));

As easy as that.