datetime to timestamp

96,467

Solution 1

$timestamp = strtotime($datetime);

Or if you're confident of the format, split up the string with explode() or even substr and pass the necessary parts into the mktime function.

Be aware that strtotime can sometimes get the timestamp wrong, if a slightly unconventional format is used.

EDIT:

A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat eg:

$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');

See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.

Solution 2

$timestamp = strtotime('12-04-2010');
Share:
96,467
medk
Author by

medk

Updated on July 28, 2020

Comments