how to make date formating consistent in all envirnment - php

I am using PHP 7.3.
I am trying to format a strtotime() format to ISO format, but the result is taking timezone of system, causing inconsistent result in different environment.
$date = date('Y-m-d\TH:i:s'.substr((string)microtime(), 1, 4).'\Z',1585526400);
In phpfiddle, the output is 2020-03-30T00:00:00.475Z, in local system 2020-03-30T05:30:00.415Z and in server it is 2020-03-30T05:00:00.415Z.
if I am changing the code to
$date = date(DATE_ISO8601, 1585526400);
phpfiddle output is 2020-03-30T00:00:00+0000, local 2020-03-30T05:30:00+0530 and in server 2020-03-30T05:00:00+0500
How I can make it consistent?

You can set timezone explicitly. Sample snippet:
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1585526400);
echo $date->format('Y-m-d\TH:i:s');

You can do this by setting the Timezone at the time of getting date and time as:
$dateTime = new DateTime('now', new DateTimeZone('UTC'));
$dateTime = $dateTime->format("y-m-d H:i:s");
echo $dateTime;

Related

Convert UTC date&time to Europe/Lisbon

I want to convert date UTC for Europe/Lisbon, but the code I have gives me different outputs/times:
$datafull = "13-04-2021 08:47:13";
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
echo $date->format('d-m-Y H:i:s (e)');
// 13-04-2021 09:47:13 (Europe/Lisbon)
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
print $datetime->format('d-m-Y H:i:s (e)');
// 13-04-2021 08:47:13 (Europe/Lisbon)
When you supply a timezone object to the DateTime constructor you're telling it in what timezone the give $datafull is. So in:
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
You say it is in Europe/Lisbon, and it stays there.
In the other code:
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
The default timezone is used when the DateTime is constructed, probably UTC on your server, and then you change it afterwards on the second line to Europe/Lisbon, which is an hour ahead.
See: DateTime::__construct

Parse a UTC date, output local datetime

I have already read How to get local time in php? and similar questions, but here I want to do the contrary:
With a given datetime in UTC (e.g. 2021-03-31 23:45:00), how to output the date in local timezone?
$dt = new DateTime("2021-03-31 23:45:00"); // parse the UTC datetime
echo $dt->format('m/d/Y, H:i:s');
In Europe/Paris timezone, it should output 2021-04-01 01:45:00, but here it sill outputs 2021-03-31 23:45:00. How to fix this?
I also tried with strtotime with a similar result; I also tried with:
date_default_timezone_set('UTC');
$dt = new DateTime("2021-03-31 23:46:14");
date_default_timezone_set('Europe/Paris');
echo $dt->format('m/d/Y, H:i:s');
without success.
You need to change the timezone of the date (using DateTime::setTimeZone()), not the default timezone:
date_default_timezone_set('UTC');
$dt = new DateTime("2021-03-31 23:46:14");
$dt->setTimeZone(new DateTimeZone("Europe/paris")); // change date timezone
echo $dt->format('m/d/Y, H:i:s');
Output:
04/01/2021, 01:46:14
Changing the default timezone affects the new DateTime(), not the format() result.
This can also be easily solved with date and strtotime:
//The following line is only required if the server has a different time zone.
//date_default_timezone_set('Europe/Paris');
$utcDate = "2021-03-31 23:45:00";
echo date('m/d/Y, H:i:s',strtotime($utcDate.' UTC'));
Output:
04/01/2021, 01:45:00

How to convert stored database utc timestamps data into local timezone in PHP

is there any chance to convert database utc zone timestamps record into local timezone in PHP?
//example of code
$utc_date = $row['utc_date'];
// common values
$timezone = "Australia/Melbourne";
$tz = new DateTimeZone($timezone);
$dt = new DateTime();
$dt->setTimezone($tz);
// this can run inside a loop or by itself
$utc_date = $row['utc_date'];
$dt->setTimestamp(strtotime($utc_date));
$datetime = $dt->format("Y-m-d H:i:s");
// use the formatted date as required
echo $datetime;
You can create a new DateTime object inside a loop if you prefer or create it outside the loop and reuse it.
If the date/times don't come out of the database in the format 2019-07-18 10:00:00+00:00 you may have to add +00:00 to the retreived date.
You can simple use following exmaple to resolve your problem
<?php
//Conside in UTC the date and time will be looks like this
$utcTime = time();
echo date("Y-m-d H:i:s", $utcTime);
//now change it to your time zone
date_default_timezone_set('Your-local-time-zone');
$utc_date = date("Y-m-d H:i:s", strtotime($row['utc_date']));
?>
Let Say for my time zone I'll use
date_default_timezone_set('Asia/Kolkata');
Also, You can find your timezone supported by PHP or not here
You can read more about PHP Date funcation from here

how to get the correct time with php

i am trying to get the correct time with php
not the one that the user's computer gives is there a function that gives the correct "GMT" time as an example .
PHP gets the server date, not the user computer date.
If you need to get a different date timezone then you need set the timezone to your desired.
See: http://php.net/manual/en/function.date.php
like:
$tz = 'Europe/London';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('d.m.Y, H:i:s');
If you just want GMT time, you can use gmdate instead of date. For example,
echo date('Y-m-d H:i:s') . "\n";
echo gmdate('Y-m-d H:i:s') . "\n";
Output (for a server in Europe)
2019-02-04 03:34:05
2019-02-04 02:34:05
Demo on 3v4l.org
You will be getting the server time only, not the computer time
$now = new DateTime(null, new DateTimeZone('America/New_York')); //timezone format
echo $now->format("Y-m-d\TH:i:sO"); // Server time

PHP Display Date Time with Offset

In php how can I display the current date and time with the timezone offset such as this:
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s");
//
This displays 2019-01-09 19:30:19
but I want it to display 2019-01-09 07:00:00 because I am in Mountain Time Zone
When working with time zones and DateTime() objects you should be using DateTimeZone() instead of date_default_timezone_set()
$now = new \DateTime(null, new DateTimeZone('America/Denver'));
echo $now->format("Y-m-d h:i:s");
Demo
Try to paste second parameter your timezone
$date = new DateTime('NOW', new DateTimeZone('America/Denver));
echo $date->format('Y-m-d h:i:s');
You could define wich timezone is fitting for you by this answer.
List of US Time Zones for PHP to use?
for getting all timezones names in you country. Then you pick your timezone and paste
$date = new DateTime('NOW', new DateTimeZone('Your Country/Your timezone'));
you have right code, but you have want to show time in 12 hours format. then you have use something like that.
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s A");

Categories