I'm trying to convert these two dates to DateTime: 16/09/2019 15:00:00 - 21/09/2019 09:00:00
but I got an incorrect DateTime for both dates:
$duration = "16/09/2019 15:00:00 - 21/09/2019 09:00:00";
$duration = explode('-', $duration);
$start = strtotime($duration[0]);
$end = strtotime($duration[1]);
echo date('d/M/Y H:i:s', $start);
echo date('d/M/Y H:i:s', $end);
result:
31/Dec/1969 16:00:00
31/Dec/1969 16:00:00
First, you need to set dates like dd-mm-yyyy Second, use delimiter for explode as like " - "
$duration = "16-09-2019 15:00:00 - 21-09-2019 09:00:00";
$duration = explode(' - ', $duration);
$start = strtotime($duration[0]);
$end = strtotime($duration[1]);
echo date('d/M/Y H:i:s', $start);
echo date('d/M/Y H:i:s', $end);
Datetime createformatformat
For example
$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');
Output
2009-02-15
Your answer
$duration = "16/09/2019 15:00:00 - 21/09/2019 09:00:00";
$duration = explode(' - ', $duration);
$date = date_create_from_format('d/m/Y:H:i:s', $duration[0]);
echo $date->getTimestamp();
$date = date_create_from_format('d/m/Y:H:i:s', $duration[1]);
echo $date->getTimestamp();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With