0

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.

J.Zil
  • 2,397
  • 7
  • 44
  • 78
  • 3
    Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Dave Feb 22 '19 at 16:28
  • `strtotime` returns a unix timestamp which, despite the title of your question, is not what you're looking for. – Federico klez Culloca Feb 22 '19 at 16:29
  • @FedericoklezCulloca what is this format called? – J.Zil Feb 22 '19 at 16:29
  • I'm not sure it has a proper name, but it looks like some form of Internet date format as described in [rfc 3339](https://tools.ietf.org/html/rfc3339) – Federico klez Culloca Feb 22 '19 at 16:32
  • Note: I'm saying it doesn't have a name because I'm not 100% sure it conforms to ISO 8601 (I'm not sure the timezone is mandatory and I'm not gonna read the whole standard to find out), which is probably the closest you could get to what you want. – Federico klez Culloca Feb 22 '19 at 16:34

4 Answers4

3

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.

Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
2

I think the format you want is ISO 8601.

Here the php code.

<?php

$unixTimestamp = date("c", strtotime('01-02-2019'));

?>

Vidal
  • 2,605
  • 2
  • 16
  • 32
1

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.

More info on DateTimeInterface constants

slothluvchunk
  • 382
  • 2
  • 9
0

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

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103