mktime() is great if you already have the individual numeric values for the date and time you want to store. However, often the PHP script will receive a date or time as a string.
For example, if the script works with emails, it may need to handle message dates, which are normally represented in the following format:
Date: Mon, 22 Dec 2008 02:30:17 +0000
Web server logs tend to use a format such as the following:
15/Dec/2008:20:33:30 +1100
Alternatively, the script might receive a user – input date along the lines of:
15th September 2006 3:12pm
Although you can use PHP’s powerful string manipulation functions and regular expressions to split such strings into their component parts for feeding to mktime(), PHP provides a useful function called strtotime() to do the hard work for you. strtotime() expects a string representing a date, and attempts to convert the string into a timestamp:
$timestamp = strtotime( “15th September 2006 3:12pm” );
You can pass in dates and times in practically any human – readable format you like. Here are some examples of valid date/time strings that you can pass to strtotime() :
Date/Time String | Meaning |
6/18/99 3:12:28pm | 3:12:28 pm on June 18 th , 1999 |
15th Feb 04 9:30am | 9:30 am on February 15 th , 2004 |
February 15th 2004, 9:30am | 9:30 am on February 15 th , 2004 |
tomorrow 1:30pm | The day after the current date at 1:30 pm |
Today | Midnight on the current date |
Yesterday | Midnight on the day before the current date |
last Thursday | Midnight on the Thursday before the current date |
+2 days | The day after tomorrow at the current time of day |
– 1 year | One year ago at the current time of day |
+3 weeks 4 days 2 hours | 3 weeks, 4 days, and 2 hours from now |
3 days | 3 days after the current date at the current time |
4 days ago | 4 days before the current date at the current time |
3 hours 15 minutes | The current time plus 3 hours 15 minutes |
As with mktime() , strtotime() assumes by default that the string you pass it represents a date and time in the computer’s time zone, and converts to UTC accordingly. However, you can specify a time in a different time zone by adding an offset from UTC, using a plus or minus sign followed by a four – digit number at the end of the string. The first two digits represent the hour’s component of the offset, and the second two digits represent the minutes.
See some of the example
$t = strtotime( “February 15th 2014, 9:30am +0000” ); // GMT
$t = strtotime( “February 15th 2014, 9:30am +0100” ); // 1 hour ahead of GMT
$t = strtotime( “February 15th 2014, 9:30am -0500” ); // Indianapolis time
$t = strtotime( “February 15th 2014, 9:30am +1000” ); // Sydney time (not DST)
$t = strtotime( “February 15th 2014, 9:30am +1100” ); // Sydney time (with DST)
strtotime() calculates relative dates (such as “ tomorrow 1:30pm ” ) based on the current date. If you want to calculate a relative date based on a different date, pass that date as a second argument to strtotime() , in timestamp format:
$localTime = strtotime( “tomorrow 1:30pm”, 0 ); // January 2nd 2013, 1:30:00 pm