Convert a date format in PHP

convert time stamp to time ago in php?
How do I use PHP to get the current year?

In this solution, you will learn a simple way to convert one date format into another date format in PHP.

The second parameter  date) needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can’t recognize.

You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn’t recognize the y-m-d-h-i-s format.

PHP 5.3 and up

Use DateTime::createFromFormat. It allows specifying an exact mask –

Using the date() syntax – to parse incoming string dates with.

PHP5.2 and lower

Parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() which will build a timestamp.

$old_date = date('l, F d y h:i:s');              // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);   

Another solution

$myDateTime = DateTime::createFromFormat('Y-m-d', $dateString);
$newDateString = $myDateTime->format('m/d/Y');

See the strtotime and date documentation on the PHP site.

convert time stamp to time ago in php?
How do I use PHP to get the current year?
en English
X
Scroll to Top