I have date in this format 'm-d-Y'
(12-31-2017). I want to convert it into a timestamp
. Normally, this works:
$timestamp = strtotime($date);
But if I am not mistaken, that works with 'd/m/Y'
and not 'm/d/Y'
.
What's the solution here?
I have date in this format 'm-d-Y'
(12-31-2017). I want to convert it into a timestamp
. Normally, this works:
$timestamp = strtotime($date);
But if I am not mistaken, that works with 'd/m/Y'
and not 'm/d/Y'
.
What's the solution here?
You can use DateTime::createFromFormat()
and then call getTimestamp()
on the Object:
//first create DateTime Object
$datetime = DateTime::createFromFormat('m-d-Y', '12-31-2017');
//get timestamp from DateTime
echo $datetime->getTimestamp();
Try this code .
<?php
$d=strtotime("12/31/2017");
echo "Created date is " . date("m-d-Y h:i:sa", $d);
?>
for more Information about date time format then read PHP Manual
You can change date according your format :
$date = '12/31/2017';
$timestamp = date('Y-m-d', strtotime($date));
$timestamp = date('d-m-Y', strtotime($date));
$timestamp = date('m-d-Y', strtotime($date));
if Timestamp
$timestamp = date('m-d-Y h:i:s', strtotime($date));
An another solution is the following:
//CONVERT the time string from the desired format
$dateTime=DateTime::createFromFormat('m/d/Y',$date);
echo "UNIX TIMESTAMP method 1".$date->format("U");
// false
echo "UNIX TIMESTAMP method 2".$date->getTimestamp();