How to Convert a String to Date in PHP?

Problem:

You want to convert an English textual date time string to a date.

Solution:

You can convert an English date time string to a date by the following two steps-

  1. Use strtotime() function to convert any English date time related text to a Unix timestamp, then,
  2. Use date() function to convert that Unix timestamp to your desired date format.

strtotime() function take an English date-time text string as parameter and returns its equivalent Unix timestamp. But, when mentioning the date and time format, you have to follow some predefined rules..

In the following example, we’ll see some string-to-date conversions-

<?php
$timestamp = strtotime("Saturday 2001-12-25");
echo date('m-d-Y',$timestamp) . "<br />";
?>

[wpdm_file id=94]

Output:
12-29-2001

How it works:
The text “Saturday 2001-12-25” inside the strtotime() function will return Unix timestamp of next Saturday after 2001-12-25. To find out next week day of a date just mention the day(ex. Saturday) before or after that date. Then, use date() function to display the timestamp in your desire format.

<?php
$timestamp = strtotime("+1 day 11 September 2012");
echo date('m-d-Y',$timestamp) . "<br />";
?>

[wpdm_file id=95]

Output:
09-12-2012

How it works:
The text “+1 day 11 September 2012” inside the strtotime() function will add one day with the 11th September 2012 and return its equivalent Unix timestamp. To add a specific number of day with a date mention plus sign then the number of days then the word “day” then the date(as you see in the example above). Similarly, to find out 5 day before a specific day, mention “-5 day date”. In place of day, you can use month, year, hour etc to find out next/previous month, year, hour etc. To find out other possibilities, see Relative Format of Date and Time

<?php
$timestamp = strtotime("first day of January 2000");
echo date('m-d-Y',$timestamp) . "<br />";
?>

[wpdm_file id=96]

Output:
01-01-2000

How it works:
The text “first day of January 2000” inside the strtotime() function will return Unix timestamp of first date of January 2000. Here, the format “first day of” sets the first day of the month(January) mentioned next. Similarly, to get the last date of the month use “last of of” format.

<?php
$timestamp = strtotime("next monday this month");
echo date('dS M Y',$timestamp) . "<br />";
?>

[wpdm_file id=97]

Output:
12th May 2014

How it works:
The string “next monday this month” inside the strtotime() function will return Unix timestamp of next Monday of the current month. To find out a date in future, use “next” formatting text. Similarly to find out a specific day of the week in the past, you can use “previous” formatting text.

<?php
$timestamp = strtotime("10 days ago today");
echo date('M dS, Y',$timestamp) . "<br />";
?>

[wpdm_file id=98]

Output:
Apr 30th, 2014

How it works:
The string “10 days ago today” inside the strtotime() function will return Unix timestamp of a day which is 10 day before today. To find out a date in past before a specific day, mention the number of days then add “ago” string. In place of “days” you can use other day based notations like “month”, “week” etc.

<?php
$timestamp = strtotime(“second Sunday of April 2014”);
echo date(‘l M d, Y’,$timestamp) . “<br />”;
?>

[wpdm_file id=99]

Output:
Sunday Apr 13, 2014

How it works:
The string “second Sunday of April 2014” inside the strtotime() function will return Unix timestamp of a day which is the second Sunday of the April 2014. To calculates x-th week day of a month(that we use in the example above), use the format “ordinal space dayname space of”. Here, First, second, third are ordinals.