I have a string which contains the date/time in it and i know its currently in Australia/Sydney timezone.
I now need to change it to GMT time.
I tried this, but its not changing the date:
$dateString = '2012-06-29 11:09:12'; // this is in Australia/Sydney timezone
$gmtDate = gmdate('Y-m-d H:i:s', strtotime($dateString));
print_r($gmtDate); // output is 2012-06-29 11:09:12
How do I subtract the offset somehow from gmt, sorry, getting a little confused.
Perhaps your web-server is not set up in the Australia/Sydney timezone ?
The following answer will help you: how to convert php date formats to GMT and vice versa?
Here is what you can do:
$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');
$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.
echo '<br/>';
// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');
Using ini_set() in your script to set the current timezone to e.g. the timezone that is used in Los Angeles, you would do this:
ini_set('date.timezone', 'America/Los_Angeles');
All subsequent requests to date and time functions would use America/Los Angeles as the timezone.
If you need to do this in your scripts, for example if the web server you are using is in a different timezone from the dates and times you wish to use in your scripts, then you should ensure the call is made at the very start of your scripts (or, if you can, move the setting out to an .htaccess file).
PHP Support Timezone, check here:
date_default_timezone_set function
List of Supporting Timezones
Ex:
date_default_timezone_set('Australia/Sydney');
echo date('Y-m-d H:i:s') . '<br/>';
date_default_timezone_set('Asia/Jakarta');
echo date('Y-m-d H:i:s') . "<br/>";
Related
So I've trying all sorts of combinations to get a date from my database (using Wordpress) to display in British Summer Time and I cannot get anything to work.
Is there any simple solution that can take the date string and make sure that in Summer Time in the UK it's an hour on from UTC time?
$classJson = $class->info;
$classJsonAsArray = json_decode($classJson, TRUE);
$classStartDate = strtotime($class->periodStart);
$classStartTime = date('H:i',$classStartDate);
So currently $class->periodStart returns: 2022-04-06 08:30:00
The time of that event should be 9.30am
All I need it to do is display the correct time, as at the moment, on the front end it displays as 8.30am.
DateTime handles timezones quite well.
$dateStringInUtc = '2022-04-06 08:30:00';
$date = new DateTime($dateStringInUtc, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('Y-m-d H:i:s'); // will output 2022-04-06 09:30:00
Working with DateTime and timezones like in the accepted answer is the better way.
But it also works with strtotime if the timezone is appended to the date string. Date then returns the local time for a timestamp.
$utcDate = '2022-04-06 08:30:00';
echo date('Y-m-d H:i:s',strtotime($utcDate.' UTC'));
I am trying to convert times to and from the following timezones, regardless of when or where the code is run:
The timezone of the running code
AEST (Australian Eastern Standard Time)
EDT (New York Eastern Daylight Time)
For example, given a unix timestamp, how do I find a new unix timestamp "monday the same week" using EDT timezone? How do I do this, such that it will always give the same result?
First you have to understand that a Unix Timestamp has nothing to do with timezones, it is always relative to UTC/GMT. To quote from the manual
Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Now that you know one timestamp represents a fixed time in reference to GMT/UTC you can go ahead and change time zones in your code to calculate time for them form the same timestamp.
Let us say you have a unix timestamp
$ts = 1171502725;
If you create a date from it you would do something like
$date = new DateTime("#$ts");
echo $date->format('U = Y-m-d H:i:s T') . "\n";
Now you want to see what does that correspond to in EST, you can do
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('U = Y-m-d H:i:s T') . "\n";
Similarly for CST
$date->setTimezone(new DateTimeZone('America/Chicago'));
echo $date->format('U = Y-m-d H:i:s T') . "\n";
And so on :)
Output
1171502725 = 2007-02-15 01:25:25 GMT+0000
1171502725 = 2007-02-14 20:25:25 EST
1171502725 = 2007-02-14 19:25:25 CST
Fiddle
You can get a list of supported timezones and their identifiers from the PHP Manual
I have stored a time in my database, for example this data: 2014-03-25 13:15:00
But when I use UNIX_TIMESTAMP(date_field) through mysql query or strtotime() in PHP, both functions output is effected by timezone.
Please tell me if it's possible to get unix time without timezone effect?
It will also be very helpful if anyone can provide a PHP function.
Please do not suggest to me to change the timezone, because this is not a suitable solution.
Current output of 2014-03-25 14:05:00 when covert to unix and then again convert to date 2014-03-25 08:35:00
UPDATE
this not only happen only to DB time. like if I directly call this :
date('Y-m-d H:i:s', strtotime(2014-03-25 14:05:00))
then it output
2014-03-25 08:35:00
With PHP you can use DateTime:
$date = new DateTime('2014-03-25 13:15:00', new DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:s'); // 2014-03-25 13:15:00
echo $date->format('U'); // 1395753300
This assumes the times in your database are expressed in UTC.
Before PHP 5.3
$date = '2014-03-25 13:15:00';
echo gmdate('Y-m-d H:i:s', strtotime("$date GMT"));
I am trying to create a select list starting from the current date of the user. I want it so that it is set to midnight in unix timestamp format.
This is all I'm doing:
$today = strtotime('today');
echo $today;
This is my result:
1333144800
which is: Fri, 30 Mar 2012 22:00:00 GMT according to Epoch Converter (incorrect by a couple hours.)
If you want strtotime() to return a timestamp relative to UTC (00:00:00 UTC instead of e.g. 00:00:00 UTC+2, if your system is set to a timezone with an offset of 2 hours against UTC/GMT), you need to specify that:
$today = strtotime('today UTC');
GMT (+0) time
echo $today = strtotime('today GMT');
echo "<br>" . $today = date("d-m-Y H:i:s", $today);
We expect that your server runs at GMT - that is the best (for maneuvering with time displays later). If not, you MUST adjust php.ini set this "date.timezone = GMT".
When you get that done, you will see 00:00 with my codes.
Then, you must develop function (ie DisplayDate()) in your script to display dates of your website correctly if
youre not in GMT area
or/and if you want for your users to see times in their timezone with timezone selection for example.
DisplayDate() should include support for daylight changes also (0, or +1 hour / summer and winter time).
strtotime( $time )
is designed to return a unix timetamp, meaning, it will return the number of seconds since jan 1, 1970. http://www.php.net/manual/en/function.strtotime.php
To get around this, use something like:
$today = date("d/m/Y H:i:s", strtotime('today'));
echo $today;
You might have to specifically set the time as well as the day:
$today_midnight = strtotime('today UTC 00:00');
You should check the timezone configuration in your php.ini file. In my case (I live in El Salvador) I had to change it like this:
date.timezone = America/El_Salvador
I have a timezone of the user(he chooses it from a list)
I have a time in UTC(not current time)
So I need something like GetTimeForRegion(time, timezone) for PHP. Is there such functions or libraries or services?
you can use DateTime::setTimezone(). If your UTC date is an UNIX timestamp, you can use some code like this :
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1297869844);
$date->setTimezone(new DateTimeZone('Europe/Paris'));
echo $date->format('Y-m-d H:i:s');
// Will print 2011-02-16 16:24:04
date('r') or date('c') may help you.
echo date('r') prints Thu, 16 Feb 2011 16:01:07 +0200
echo date('c') prints 2011-02-16T16:01:07+02:00
You need to look at the Date/Time API in PHP. I strongly advise you to stay away of gmdate and older date functions in php.
In your case, you should ask the user for its Olson based time zone.
The code of Artefact2 will do the trick.
please write this instead :
$date = date("Y-m-d H:i:s" , time());
so Y it means year m means month d means day
H get hours from 0 - 24
h get hours from 0 to 12
i get minutes
s get seconds