-1

I want to convert a date string to a specific date format I'm currently using strtotime to convert a date, but it doesn't get the correct hour

My code

<?php
        $timestamp = 'Fri, 10 Aug 2018 11:00:00 GMT ';

    $formatted = strtotime($timestamp);

    echo date('Y-m-d H:i:s', $formatted);
?>

It gives me 01:00 pm instead of 11:00 Am i doing something wrong?

jcortes
  • 1
  • 2
  • Let me guess, you're in a UTC+2 timezone currently? – fvu Aug 11 '18 at 00:09
  • i thought it could be the problem, i'm on utc-3 actually. Any solution? – jcortes Aug 11 '18 at 00:11
  • Could you try `echo date('Y-m-d H:i:s P', $formatted);` to see what timezone your computer thinks it's in? – fvu Aug 11 '18 at 00:12
  • I can't figure out the source's (from the string) timezone, but mine's says +02:00 – jcortes Aug 11 '18 at 00:20
  • 1
    So although you are in UTC-3, your computer thinks it's in UTC+2? Seems like a misconfiguration, and I'd strongly suggest to solve the problem there instead of overriding the timezone. It will be a lot easier to maintain that way. – fvu Aug 11 '18 at 00:22
  • No, it's +02:00 actually, see i'm getting this datetime string from an api, what i want is to record in a database, but mysql only supports yyyy:mm:dd h:m:s format, so i want to convert, but if i convert it as it is it's going to record 01:00 pm, is there any solution i can use? – jcortes Aug 11 '18 at 00:25
  • So, `$timestamp` is like what you're getting from the API (including the GMT string?), in that case you can do as @BojanSrbinoski suggest, and select time zone `UTC` there. However, as you're definitely not in UTC, by doing this you skew the timestamps by whatever your offset from UTC is. – fvu Aug 11 '18 at 00:30
  • By doing this i should guess the correct UTC right? Yes it comes with GMT string on the end :( I still don't get how can i get the correct date conversion so i can record it – jcortes Aug 11 '18 at 00:45
  • I managed to fix it by adding date_default_timezone_set('UTC'); to header. – jcortes Aug 11 '18 at 00:50
  • *"but mysql only supports yyyy:mm:dd h:m:s format"* - About what you said; MySQL supports other formats also, such as a UNIX timestamp for instance. Here @jcortes have a look https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html and more precisely https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp as well as UTC. – Funk Forty Niner Aug 11 '18 at 03:02
  • @jcortes You should have probably have mentioned that a (mysql) database was part of the picture here, unless you think it's not important. – Funk Forty Niner Aug 11 '18 at 03:08

1 Answers1

0

php.ini

date.timezone=UTC

or

.php file

echo date('Y-m-d H:i:s T', time())."\n";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T', time())."\n";

or

.htaccess file

php_value date.timezone UTC

--

If the timezone you are using is not UTC, check the following urls:

http://php.net/manual/en/timezones.php https://publib.boulder.ibm.com/tividd/td/TWS/SC32-1274-02/en_US/HTML/SRF_mst273.htm

Mert Aşan
  • 366
  • 1
  • 6
  • 18