I need to convert a given GMT date and time (YYYY-MM-DD HH:MM) into such a string YYYYMMDD representing Eastern coast date. Do you think the code below is OK?
$date='2011-11-07 04:30';
$date.='-4 hours';
$date=strftime('%Y-%m-%d %H:%M',strtotime($date));
$y=gmdate('Y');
$date2=date('Y-m-d 02:00',strtotime($y.'-03-01 second sunday'));
$date3=date('Y-m-d 02:00',strtotime($y.'-11-01 first sunday'));
if($date<=$date2||$date>=$date3) {
$date.='-1 hour';
$date=strftime('%Y-%m-%d %H:%M',strtotime($date));
}
$date=date('Ymd', strtotime($date));
I'd advise against trying to do the calculation yourself. There's too many nuances with our wonderful, ever-changing, time standard. Instead, rely on PHP to perform the calculations by using php.net/date_default_timezone_set to set your timezone to GMT, then strtotime() (or the DateTime class) to get the unix-timestamp value.
Once you have the unix timestamp, use php.net/date_default_timezone_set again to set the timezone to America/New_York and use date()
Related
Short question but I can't get my finger on it. This piece of code:
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date));
returns Mon while
$date = '2015-12-08T00:00:00';
echo date('D', strtotime($date));
returns Tue. Why is that? The +01:00 is for the timezone, but that should not affect the day in my opinion.
First I've looked up that 08-12-2015 is in fact a Tuesday, so now we know the first one is incorrect.
PHP's date() is an Unix timestamp according to their own docs.
My belief is that adding the +1 as a timezone triggers the calculation to the +0 timezone (UTC) when asking for the day of the week and therefore returns 23:00 on monday as the current UTC time.
You can specify the timezone before executing the rest of the code: date_default_timezone_set('Europe/Amsterdam');
<?php
date_default_timezone_set('Europe/Amsterdam'); //this is an example of a +1 timezone, choose one from http://php.net/manual/en/timezones.php
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date) );
?>
strtotime will parse your date string using the supplied time zone or using the default timezone if unspecified. We can't see from the code you've posted what time zone your server is configured to, but once the date is parsed and converted to your time zone, the time may legitimately occur in the previous day, hence why you're seeing 'Mon'.
Either supply a time zone in the strtotime call via the now argument or set one globally with date_default_timezone_set.
I am trying to convert a date to a timestamp at the exact midnight point.
To do this, I am using the following little function.
function converttotimestamp($date)
{
$date = str_replace('/', '-', $date);
$date = $date.' 00:00:00';
$date = DateTime::createFromFormat('m-d-Y H:i:s',$date);
return $date->getTimestamp();
}
So as you can see, I am attaching a midnight time at the end.
I tried using this as shown below
echo converttotimestamp('7/22/2014');
So as you would expect when you run this in a unix converter, you would get 1405987200.
But In my case it returns 1405976400 whicj translates to Mon, 21 Jul 2014 21:00:00.
Oh. I am in Kenya.
The reason you may be seeing a different time returned than the one you were expecting is likely because you haven't considered the relevant timezones. There are a couple different ways you can set the timezone. You can set it during runtime:
http://php.net/manual/en/function.date-default-timezone-set.php
You can set it in your PHP config file:
http://php.net/manual/en/datetime.configuration.php#ini.date.timezone
Or you can set the timezone of your DateTime object:
http://php.net/manual/en/datetime.settimezone.php
Whenever you are converting between dates, you must consider the relevant timezone, as this is the only way for the system to determine how to switch between date formats, make comparisions and output specific dates and times. For example, if you want to convert a date and time to a timestamp, the system must know the timezone of the input date and time so it can convert properly. Take a look at strtotime:
http://us2.php.net/manual/en/function.strtotime.php
Unix timestamps are GMT timezone, so make sure you convert your datetimes accordingly. HTH.
I do
strtotime("2008-09-04")
Where 09 is the month and 04 is the day and I get the result:
1220500800
Which is Thu, 04 Sep 2008 04:00:00 GMT. Where does those 4 hours come from? I should get 1220486400 instead of 1220500800 from strtotime.
You can set timezone globally with function date_default_timezone_set('UTC');, or you can just set timezone locally when you call strtotime() function like:
echo strtotime("2008-09-04 +0000"); # 1220486400
As people above have said, you're likely suffering from a time zone mismatch. This function may be of use in debugging the issue: http://us3.php.net/date_default_timezone_get
The most common problems with PHP's strtotime are timezones and date-time formats. I will address those 2 points.
First the format. As suggested by others on stackoverflow use the iso 8601 date format YYYY-MM-DD (https://www.iso.org/iso-8601-date-and-time-format.html). For example, September 27, 2012 is represented as 2012-09-27.
Next the timeszones. The best of all , do not use any timezone use the Universal Coordinated Time UTC (which is universal time and corresponds with GMT without Daylight Saving). UTC is the time standard commonly used across the world. The world's timing centers have agreed to keep their time scales closely synchronized - or coordinated - therefore the name Coordinated Universal Time (https://www.timeanddate.com/time/aboututc.html).
So now we have the picture clear. To convert a date time into unixtimestamp do:
// Saves your local set Timezone.
$saveDDTZ = date_default_timezone_get();
// Sets the timezone to UTC
date_default_timezone_set("UTC");
// Converts a date-time into a unix timestamp (= 1560470400).
$unixTS = strtotime( "2019-06-14 00:00:00 UTC");
// Restore the timezone
date_default_timezone_set($saveDDTZ");
To restore a unix timestamp to a Date use:
// Saves your local set Timezone.
$saveDDTZ = date_default_timezone_get();
// Sets the timezone to UTC
date_default_timezone_set("UTC");|
$unixTS = 1560470400
$dateTime = date("Y-m-d H:i:s", $unixTS);
// Restore the timezone
date_default_timezone_set($saveDDTZ");
If you want to change the UTC date into a TimeZone use the difference of the UTC and the TimeZone and the Daylight Saving.
I'm aware of two ways to get a timestamp from a relative date/time string in PHP:
strtotime: Allows user to specify their own $now value
DateTime object: Allows user to specify their own $timezone value
Is there a way to get a timestamp from a date/time string that allows one to specify both the timezone AND the $now value? It seems like the only way would be to use strtotime while temporarily overriding the default timezone for the entire php application and then setting it back immediately afterwards. That just seems like a hacky solution, and it would be nice if there were a cleaner way.
Edit: there seems to be some misunderstanding about what I'm trying to do. Here's a more concrete example:
"I want to find the timestamp corresponding to the string 'next tuesday at 3:00pm' within the America/Los_Angeles timezone AND specifying an arbitrary value for $now, such as March 14th, 2014 at 8:05am."
I've prepared an example. This may be want you want:
<?php
// Including the timezone int the time strings (thanks #Mike B!!!)
// will make it very easy. just strtotime() is required
// create a timestamp for March 14th PDT
$now = strtotime('14 March 2014 8:05am America/Los_Angeles');
// get the requested timestamp.
$nexTuesday = strtotime('next tuesday 3:00 pm America/Los_Angeles', $now);
For example, I have a date time in this format and its UTC offset is +3:
2011-02-23 05:00:00
So its UTC time is actually:
2011-02-23 02:00:00
But what is the simplest way to do this conversion in PHP?
I have thought of using the DateTimeZone class but as you can see, I only have the offset but not the timezone name. And to construct a DateTimeZone object, the timezone name is required.
Maybe there are some better ways without using DateTimeZone class.
Thanks in advance.
Could just modify the date based on the offset. If you are interested in changing this to other timezones then create the date with the UTC timezone.
$date = new DateTime('2011-02-23 05:00:00', new DateTimeZone('UTC'));
$date->modify(sprintf('%s%d hours', $offset < 0 ? '+' : '-', $offset));
Note: This will work even if the offset is a string. i.e. "-3" or "+3"
Then for example if you wanted to see that time in Melbourne, Australia.
$date->setTimezone(new DateTimeZone('Australia/Melbourne'));
echo $date->format('c');
You can use date() and strtotime() for this.
$in = "2011-02-23 05:00:00";
$offset = "-3 hours";
$out = date('Y-m-d H:i:s',strtotime("$in $offset"));
It's also good to note that with this, you aren't restricted on what you're offsetting. The strtotime() function lets you do all kinds of wonderful things, like "-2 weeks +3 days ...", etc.
You are also free to get any date format from date(), or just use strtotime() alone to get the timestamp.