I have an user with the timezone (for example: +7 GMT).
In PHP language:
My server is set timezone at 0-GMT.
How could I know from my server that now is midnight of that user?
Assuming you have the timezine of the user saved (I'll use America/Los_Angeles since that is currently GMT -7 right now)
$user_tz = 'America/Los_Angeles'; // get from your db
$dt = new DateTime();
$dt->setTimeZone(new DateTimeZone($user_tz))
if ($dt->format('g') == 0 && $dt->format('i') == '00')
{
echo "it's midnight";
}
You can't with PHP since it isn't running on the users computer, another option wold be using the getTimezoneOffset() in javascript.
Related
I need to send an email to users based wherever in the world at 9:00 am local time. The server is in the UK. What I can do is set up a time difference between each user and the server's time, which would then perfectly work if DST didn't exist.
Here's an example to illustrate it:
John works in New York, -5 hours from the server (UK) time
Richard works in London, UK, so 0 hour difference with the server.
When the server goes from GMT to GMT +1 (BST) at 2:00am on a certain Sunday, this means that John now has a -6H time difference now.
This scenario I can still handle by updating all the users outside the server's local time, but once I've moved forward/backward the time of all the other users, I still need a way to detect when (time and date) the users living outside the UK will (or will not) change their local time to a probable DST one.
I need a PHP method to know/detect when other parts of the world will enter/exit DST.
Do you need to know all the details of DST transition yourself? or do you just need to know when is 9:00 am in a given timezone?
If it's the latter, PHP can use your operating system's timezone database to do that for you. The strtotime() function is remarkably good at "figuring out" what you mean:
echo strtotime("today 9:00 am America/New_York"); // prints "1306501200"
echo strtotime("today 9:00 am Europe/London"); // prints "1306483200"
Just make sure you're using one of the PHP supported timezones.
As Jimmy points out you can use timezone transitions, but this is not available on PHP <5.3. as dateTimeZone() is PHP>=5.2.2 but getTransitions() with arguments is not! In that case here is a function that can give you timezone data, including whether in DST or not.
function timezonez($timezone = 'Europe/London'){
$tz = new DateTimeZone($timezone);
$transitions = $tz->getTransitions();
if (is_array($transitions)){
foreach ($transitions as $k => $t){
// look for current year
if (substr($t['time'],0,4) == date('Y')){
$trans = $t;
break;
}
}
}
return (isset($trans)) ? $trans : false;
}
Having said that, there is a simpler method using date() if you just need to know whether a timezone is in DST. For example if you want to know if UK is in DST you can do this:
date_default_timezone_set('Europe/London');
$bool = date('I'); // this will be 1 in DST or else 0
... or supply a timestamp as a second arg to date() if you want to specify a datetime other than your current server time.
Changing my answer a bit: DateTimeZone::getTransitions looks like it will do what you need, provided you have PHP >= 5.2.
From a comment in the documentation:
<?php
$theTime = time(); // specific date/time we're checking, in epoch seconds.
$tz = new DateTimeZone('America/Los_Angeles');
$transition = $tz->getTransitions($theTime, $theTime);
// only one array should be returned into $transition. Now get the data:
$offset = $transition[0]['offset'];
$abbr = $transition[0]['abbr'];
?>
So here, all we need to do is pass in the timezone we want to check and we can know if that timezone is in DST/what the offset is. You'll then need to check the offset against GMT to see if you want to send your e-mail now, or not now.
I created this PHP function to see if a time value is BST or not:
<?php
function isBST($timestamp)
{
$baseDate = date("m/d/Y H:i:s", $timestamp);
$clocktime=strtotime($baseDate." Europe/London")."\n";
$utctime=strtotime($baseDate." UTC")."\n";
//echo "$a \n$b \n";
if ($clocktime!=$utctime)
{
return true;
}
else
{
return false;
}
}
?>
I don't really understand how the datetime works.
for example if I create a dateTime
$date = new DateTime();
echo $date->('H');
Did the hour will be generated in function to the server time or the user time ?
If this is the server timezone, is there a way to convert it to the user timezone
Thanks for your help
PHP is server side. The hour is generated is server time.
If you want client's(user) time you must use JavaScript.
You maybe want to use JavaScript.
example
echo "<script type=\"text/javascript\">";
echo "localtime = new Date();";
echo "alert(localtime.getHours());";
echo "</script>";
knowing the current server date such
$current = strtotime(date("Y-m-d H:m:s")) //return timestamp
can add or subtract hours to result your time to set the current date variable at the user level:
$new_time = date($current, strtotime('+5 hours')
I need to send an email to users based wherever in the world at 9:00 am local time. The server is in the UK. What I can do is set up a time difference between each user and the server's time, which would then perfectly work if DST didn't exist.
Here's an example to illustrate it:
John works in New York, -5 hours from the server (UK) time
Richard works in London, UK, so 0 hour difference with the server.
When the server goes from GMT to GMT +1 (BST) at 2:00am on a certain Sunday, this means that John now has a -6H time difference now.
This scenario I can still handle by updating all the users outside the server's local time, but once I've moved forward/backward the time of all the other users, I still need a way to detect when (time and date) the users living outside the UK will (or will not) change their local time to a probable DST one.
I need a PHP method to know/detect when other parts of the world will enter/exit DST.
Do you need to know all the details of DST transition yourself? or do you just need to know when is 9:00 am in a given timezone?
If it's the latter, PHP can use your operating system's timezone database to do that for you. The strtotime() function is remarkably good at "figuring out" what you mean:
echo strtotime("today 9:00 am America/New_York"); // prints "1306501200"
echo strtotime("today 9:00 am Europe/London"); // prints "1306483200"
Just make sure you're using one of the PHP supported timezones.
As Jimmy points out you can use timezone transitions, but this is not available on PHP <5.3. as dateTimeZone() is PHP>=5.2.2 but getTransitions() with arguments is not! In that case here is a function that can give you timezone data, including whether in DST or not.
function timezonez($timezone = 'Europe/London'){
$tz = new DateTimeZone($timezone);
$transitions = $tz->getTransitions();
if (is_array($transitions)){
foreach ($transitions as $k => $t){
// look for current year
if (substr($t['time'],0,4) == date('Y')){
$trans = $t;
break;
}
}
}
return (isset($trans)) ? $trans : false;
}
Having said that, there is a simpler method using date() if you just need to know whether a timezone is in DST. For example if you want to know if UK is in DST you can do this:
date_default_timezone_set('Europe/London');
$bool = date('I'); // this will be 1 in DST or else 0
... or supply a timestamp as a second arg to date() if you want to specify a datetime other than your current server time.
Changing my answer a bit: DateTimeZone::getTransitions looks like it will do what you need, provided you have PHP >= 5.2.
From a comment in the documentation:
<?php
$theTime = time(); // specific date/time we're checking, in epoch seconds.
$tz = new DateTimeZone('America/Los_Angeles');
$transition = $tz->getTransitions($theTime, $theTime);
// only one array should be returned into $transition. Now get the data:
$offset = $transition[0]['offset'];
$abbr = $transition[0]['abbr'];
?>
So here, all we need to do is pass in the timezone we want to check and we can know if that timezone is in DST/what the offset is. You'll then need to check the offset against GMT to see if you want to send your e-mail now, or not now.
I created this PHP function to see if a time value is BST or not:
<?php
function isBST($timestamp)
{
$baseDate = date("m/d/Y H:i:s", $timestamp);
$clocktime=strtotime($baseDate." Europe/London")."\n";
$utctime=strtotime($baseDate." UTC")."\n";
//echo "$a \n$b \n";
if ($clocktime!=$utctime)
{
return true;
}
else
{
return false;
}
}
?>
HI All,
I'm trying to match today's date and time at Atalanta to a database value. I'm testing following code.
$date = new DateTime();
$newToday = $date->format('Y-m-d H:i:s');
$dateTimeArr = split(" ",$newToday);
$dateArr = split("-", $dateTimeArr[0]);
$timeArr = split(":",$dateTimeArr[1]);
$testTime = date("Y-m-d H:i",mktime($timeArr[0]+4, $timeArr[1], $timeArr[2], $dateArr[1], $dateArr[2], $dateArr[0])); // 4 is Daylight Saving Time offset
When I run the code, I found that there is 1 hour time difference if I check the time at http://www.timetemperature.com/tzga/atlanta.shtml
I'm adding the day light saving offset which 4 hours, but still the time I get is 1 hour more than the actual time. Why this difference is seen ? How to rectify this ?
EDIT
My server is at different time zone than Atalanta. I want to handle the time difference without knowing the timezones. For this, for each city we have added timezone offset in database.
Use the DateTime and DateTimeZone objects:
//EDT = Eastern Daylight Saving Time
$x = new DateTime(null, new DateTimeZone('EDT'));
echo $x->format("Y-m-d H:i")
I think that if you use America/New_York (which is in the same timezone as Atlanta) as the timezone, it will change accordingly between normal and daylight saving time.
$x = new DateTime(null, new DateTimeZone('America/New_York'));
echo $x->format("Y-m-d H:i")
Both versions output the same time for me.
Have you checked your server's (the computer that executes the actual PHP script) time? See if it has the right time
I store timestamps on my server using a simple timestamp in SQL. When I pull down that timestamp, I run it through the following function in order to format the time.
How do I add to the following function in order to convert $timestamp into whatever timezone the user is querying from?
// Returns the formatted time
function displayDate($timestamp)
{
$secAgo = time() - $timestamp;
// 1 day
if ($secAgo < 86400)
return date('h:i:A', $timestamp);
// 1 week
if ($secAgo < (86400 * 7))
return date('l', $timestamp);
// older than 1 week
return date('m/t/y', $timestamp);
}
If you have timezone name to change, you can use something like:
$date = new DateTime(date('Y-m-d H:i:s', $timestamp), new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Asia/Vladivostok'));
return $date->format('m/t/y');
Where Asia/Vladivostok is the user's custom timezone.
#zerkms's method is the best way to convert timestamps to any timezone.
But if you do that every time you need to display a time, it may have a significant performance hit because you're setting up and tearing down a timezone object every time. There's a convenient shortcut, if all the timestamps on the page are going to be in the same timezone. (Which is usually the case, because users don't change timezones in the middle of an HTTP request.)
Somewhere outside of the function, do:
date_default_timezone_set('America/New_York'); // or any other timezone
It's probably a good idea to tie this into one of your session management functions, so that the same user always gets the same timezone.
Once you do this, date() will automatically start using the correct timezone, assuming that $timestamp represents a Unix timestamp. It will also automatically correct for daylight saving time. So there's no need to change anything in that function.
Use timezone_identifiers_list() to retrieve a list of valid timezones.