How to get time and date from datetime stamp in PHP?

You could use the strtotime function, as long as the dates are after 1/1/1970 -

<?php

$s = strtotime('8/29/2011 11:16:12 AM');

$date = date('m/d/Y', $s);
$time = date('H:i:s A', $s);

?>

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

strtotime creates a UNIX timestamp from the string you pass to it.


E.g.

<?php
$s = '8/29/2011 11:16:12 AM';
$dt = new DateTime($s);

$date = $dt->format('m/d/Y');
$time = $dt->format('H:i:s');

echo $date, ' | ', $time;

see http://docs.php.net/class.datetime


edit: To keep the AM/PM format use

$time = $dt->format('h:i:s A');

<?php
    $date = strtotime('8/29/2011 11:16:12 AM');
    $dat = date('m/d/y', $date);
    $tme = date('H:m:s A',$date);

?>

For more information about date() function, plz visit http://php.net/manual/en/function.date.php

Tags:

Php

Date