0

I have a variable like this

$string = '2019-10-07T19:00:00+07:00';

it is in ISO8601 format date time. how can I get the date or the time only?

or does any method to convert this into another format?

like:

$str = '2019-10-07'

Thank you for your help!

Tùng Nguyễn
  • 103
  • 1
  • 14
  • 1
    Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Dharman Oct 06 '19 at 16:43

2 Answers2

2

Easiest way is to transform your string to a php time (integer) then display only year, month and day from this time.

 $string = '2019-10-07T19:00:00+07:00';
 echo $string;
 echo "</br>";
 echo date("Y-m-d", strtotime($string));

 > result :
 > 2019-10-07T19:00:00+07:00
 > 2019-07-10

https://www.php.net/manual/en/function.date.php

https://www.php.net/manual/en/function.strtotime.php

hope it helps

Clem Mi
  • 66
  • 5
  • Please use English links when referencing external resources. Yours are in French. – Funk Forty Niner Oct 06 '19 at 17:58
  • My bad i didn’t check the language, and corrected the missing quote – Clem Mi Oct 06 '19 at 18:00
  • The input string contains a time zone information (+07:00). The locale settings of the server must match this time zone. Otherwise, the result may be a different date or time. – jspit Oct 07 '19 at 06:43
0

The input string contains time zone information (+07: 00). The DateTime class ensures that you work with this time zone regardless of the locale settings of the server.

$string = '2019-10-07T01:00:00+07:00';
$dt = date_create($string);

var_dump($dt);
//object(DateTime)#1 (3) { ["date"]=> string(26) "2019-10-07 01:00:00.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+07:00" }

echo $dt->format('Y-m-d');
//2019-10-07

date() uses the local time zone of the server. If this does not match the timezone of the input string, then the result will be wrong:

setlocale(LC_ALL, 'nl_NL');

$string = '2019-10-07T01:00:00+07:00';
echo date("Y-m-d H:i:s",strtotime($string));

Output

2019-10-06 20:00:00
jspit
  • 7,276
  • 1
  • 9
  • 17