I am trying to import timestamps from an SQL table. The timestamps are in UTC, and I need to convert them to amsterdam local time. Currently I am using Datetime->format()
, but I hear from all over the place that I should be using strftime()
instead. I do indeed want to use strftime()
since that seems the only method that respects language.
I'm having trouble converting DateTime objects to timestamps, and I'm also having trouble changing the formats' language.
setlocale(LC_TIME, "nl_NL");
$dbdate = "2019-06-24 18:02:30"; //SQL UTC timestamp
$date = DateTime::createFromFormat("Y-m-d H:i:s", $dbdate, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Europe/Amsterdam'));
$fdate0 = $date->format('l H:i Y-m-d');
$fdate1 = strftime("%A %R %F",$date->getTimestamp());
echo $fdate0." compared to ".$fdate1;
The actual output:
Monday 20:02 2019-06-24 compared to Monday 11:02 2019-06-24
Expected output:
Maandag 20:02 2019-06-24 compared to Maandag 20:02 2019-06-24
Note that the strftime method somehow misplaced 9 hours.
Edit: the exact same code now gives different results. strftime still loses 2 hours, but correct locale.
Previously I ran the code on http://sandbox.onlinephpfunctions.com/. Now I ran the code on my hosted server space.
$sql = "SELECT `timestamp` FROM `mytimes`
WHERE `id` =$cid;";
$retval = mysqli_query($conn,$sql);
if(!$retval){
die("Could not get data: ".mysqli_error());
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)){
setlocale(LC_TIME, "nl_NL");
$dbdate = $row["timestamp"];
$date = DateTime::createFromFormat("Y-m-d H:i:s", $dbdate, new
DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Europe/Amsterdam'));
//$fdate0 = $date->format('l H:i Y-m-d');
$fdate1 = strftime("%A %R %F",$date->getTimestamp());
}
This code now gives timestamps akin to Maandag 18:02 2019-06-24
, which is in the correct language, but still UTC time for some reason. Why did it lose the Eu timezone???