I have my date in the following format:
01-02-2019
and I need to get it into the following format:
2019-02-01T00:00:00.000
Is it strtotime I need? I find that a bit confusing to use.
strtotime("01-02-2019");
Thank you for your help.
I have my date in the following format:
01-02-2019
and I need to get it into the following format:
2019-02-01T00:00:00.000
Is it strtotime I need? I find that a bit confusing to use.
strtotime("01-02-2019");
Thank you for your help.
As @Vidal said, it's look like ISO 8601 but it's a bit different on your example. If this is the exact result you need, here's how:
echo \DateTime::createFromFormat('d-m-Y','01-02-2019')->format('Y-m-d\TH:i:s.v');
will display : 2019-02-01T17:38:33.000
More about the format parameters: http://php.net/manual/en/function.date.php
Edit: I would recommend Vidal answer if this exact format is not mandatory since it's respecting an ISO norme.
I think the format you want is ISO 8601.
Here the php code.
<?php
$unixTimestamp = date("c", strtotime('01-02-2019'));
?>
As Vidal said in their answer, I think the ISO 8601 standard is what you're after.
I personally prefer the OOP approach, and would recommend always using PHP's DateTime()
class over strtotime()
, but either will do what you're looking for.
To do the same formatting with DateTime, simply instantiate the DateTime object, and format it as you would with strtotime()
like so:
// Build a date object
$myDate = new DateTime('01-02-2019');
// Format and display in the ISO 8601 standard using
// the 'c' format option.
echo $date->format('c');
The magic here is in the 'c' format string, which represents the ISO 8601 constant available to the DateTime class.
You can do this way if you want to convert it to another format
<?php
// Create a new DateTime object
echo date('Y-m-d\TH:i:s.v',strtotime('01-02-2019'));
?>
OR
<?php
// Create a new DateTime object
$date = DateTime::createFromFormat('d-m-Y', '01-02-2019');
echo $date->format('Y-m-d\TH:i:s.v');
?>
DEMO1: https://3v4l.org/IlCur
DEMO2: https://3v4l.org/6CMKT