Date Math

Add Day

$date = date(‘m/d/Y’,strtotime($date.’ + 2 days’));

[compare difference]
$date = date(‘Y-m-d’,strtotime(date(‘Y-m-d’).’ + 10 days’));
$priority=””;if(strtotime($date)>strtotime($daterequested)) $priority=”!”;

[Subraction]
$date = date(‘Y-m-d’,strtotime(date(‘Y-m-d’))-(10*24*60*60));

<?php
echo (strtotime(“2009-09-28”)-strtotime(date(“Y-m-d H:i:s”)))/3600;
echo”<br>”;
echo (31*24);
?>

Current Date

$now=date(‘Y-m-j’);
echo date(“Y-m-d h:i:sa”);
mysql
MySQL FORMAT (yyyy-mm-dd)

Finding a Past or Future Date

In this guide we`re going to look at how to add a certain period onto a date. As an example, we`re going to find the date 30 days from the 26th January 2003.

To work with dates list this we have to use the PHP mktime() function, which creates a timestamp. A timestamp is a number which represents the number of seconds since January 1st 1970. The format for the mktime command is:

$timestamp = mktime(hour,minute,second,month,day,year)
So, to turn our date, 26/01/2003 into a timestamp, we first need to split the date into its individual units:

<?php
$date = “26/01/2003”;
$dateSplit = explode(“/”,$date);
$day = $dateSplit[0];
$month = $dateSplit[1];
$year = $dateSplit[2];
?>
Now, $day contains “26”, $month contains “01” and $year contains “2003”

Next, we can put these date parts into the mktime function to generate a timestamp.

<?php
$timestamp = mktime(0,0,0,$month,$day,$year);
?>
$timestamp will not contain the number of seconds between January 1st 1970 and 26th January 2003 ( which is 1043539200 )

To find the date 30 days ahead, we simply have to calculate the number of seconds in a day, multiply that by 30, and add the value to the timestamp.

<?php
$oneDay = 24 * 60 * 60;
$total = 30 * $oneDay;
$newTimestamp = $timestamp + $total;
?>
Now we have a timestamp that represents the date 30 days on from 26/01/2003. All that needs to be done now is to convert the timestamp back into date format, which we do with the php date() command, as shown below.

<?php
$date = date(“d/m/Y”,$newTimestamp);
echo $date;
?>
When run in a browser, you`ll see the following date displayed: 25/02/2003

And this is the date 30 days from our target date, 26/01/2003. Using this technique you can find any future or past date, by converting the article into seconds and adding it, or taking it away from, the target date timestamp.

 Get date/time information

Description
array getdate ( [int timestamp])

Returns an associative array containing the date information of the timestamp, or the current local time if no timestamp is given, as the following associative array elements:

Table 1. Key elements of the returned associative array

Key Description Example returned values
“seconds” Numeric representation of seconds 0 to 59
“minutes” Numeric representation of minutes 0 to 59
“hours” Numeric representation of hours 0 to 23
“mday” Numeric representation of the day of the month 1 to 31
“wday” Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
“mon” Numeric representation of a month 1 through 12
“year” A full numeric representation of a year, 4 digits Examples: 1999 or 2003
“yday” Numeric representation of the day of the year 0 through 366
“weekday” A full textual representation of the day of the week Sunday through Saturday
“month” A full textual representation of a month, such as January or March January through December
0 Seconds since the Unix Epoch, similar to the values returned by time() and used by date(). System Dependent, typically -2147483648 through 2147483647.

Example 1. getdate() example

<?php
$today = getdate();
echo $today[‘year’];
print_r($today);
?>
The output will look similar to:
Array
(
[seconds] => 40
[minutes] => 58
[hours]   => 21
[mday]    => 17
[wday]    => 2
[mon]     => 6
[year]    => 2003
[yday]    => 167
[weekday] => Tuesday
[month]   => June
[0]       => 1055901520
)


Make Time

int mktime ( [int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst]]]]]]] )

$tomorrow  = mktime(0, 0, 0, date(“m”)  , date(“d”)+1, date(“Y”));
$lastmonth = mktime(0, 0, 0, date(“m”)-1, date(“d”),  date(“Y”));
$nextyear  = mktime(0, 0, 0, date(“m”),  date(“d”),  date(“Y”)+1);