PHP - Adjust Hour pulled from RSS feed for Daylight Savings - php

I've got a PHP script which pulls from an RSS feed and produces a series of outputs based on events. The script was working fine until last week, when daylight savings changed.
The script is working off an RSS feed which has the date in the following structure:
pubDate: Thu, 04 Apr 2013 19:05:00 GMT
I needed to create different php scripts for each day and time, so I used the following:
$expiry = $item->pubDate;
$expiryLenght = strlen("$expiry");
$timeStart = $expiryLenght - 12;
$time = substr("$expiry","$timeStart", 5);
$hour = substr("$time",0, 2);
$day_alpha = substr("$expiry",0, 3);
$day = substr("$expiry",5, 2);
$month = substr("$expiry",8, 3);
$year = substr("$expiry", 12, 4);
$month = strtolower($month);
$month_num = monthNum($month);
The important part for me is the '$hour' line. I need to try and adjust that for daylight savings. The pubDate is always in GMT un-adjusted.
I was thinking of going down the route of defining the daylight savings period, and if it is within the timeframe, add 1 to the hour. However, I'm not sure that that is a good solution.
Any help would be greatly appreciated,
Dave.
After much help from the comments, I ended up using this
$expiry1 = $item->pubDate;
$expiry = date('D, d M Y H:i:s \off', strtotime($expiry1 . " Europe/Dublin"));
Bit of a hackjob, but it put the Expiry back into the format it was previously in, which meant the rest of the code could work off it.

<?php
$expiry = "Thu, 04 Apr 2013 19:05:00 GMT";
echo date('l jS \of F Y h:i:s A', strtotime($expiry . " America/New_York"));
?>

I'm at work right now so can't test this but I believe you want to do something along these lines:
<?php
$timestring = 'Thu, 04 Apr 2013 19:05:00 GMT';
$mytimezone = new DateTimeZone('Pacific/Nauru');
$datetime = date_create_from_format('D, d M Y h:i:s T', $timestring, $mytimezone);
echo $datetime->format('Y-m-d H:i:sP');
This creates a DateTime object from the string formatted as you specified, but with the Timezone you choose. So you choose the Timezone, it parses the date/time from your RSS feed and converts it to the timezone of your choosing.
It's also a lot less code than what you're currently doing. :-)

Related

How to find current time of a region from a old time string in php?

I am getting time string from some mails date header. I need to get how much second before the mail has been sent to user.
Here is my code that I am trying to solve the problem.
<?php
//time string found in mail date header
$time_string = 'Wed, 15 Nov 2017 14:50:53 -0800 (PST)';
//when i use 'D, j M Y G:i:s O (e)' as identifier then date_create_from_format returns false
$mail_date = explode(" ", $time_string);
unset($mail_date[6]);
$dt_in = date_create_from_format('D, j M Y G:i:s O', implode(" ", $mail_date));
$time_in = strtotime(date_format($dt_in, 'd-m-Y H:i:s')) ; // This time may be according to my time zone (that is wrong need according to that time zone '-0800')
//returns time zone that is not compatible with php
$time_zone = date_format($dt_in, 'T'); //returns GMT-0800
//rest of the code not working
date_default_timezone_set($time_zone); //date_default_timezone_set(): Timezone ID 'GMT-0800' is invalid
$difference = time()-$time_in;
print_r($difference);
?>
It is not impotent to correct this code or do it like this. All I need to get the time difference. Any solution will be appreciable. Thanks in advance.
Convert date from email to a timestamp and compare with the current timestamp:
$time_string = 'Wed, 15 Nov 2017 14:50:53 -0800 (PST)';
$mail_date = explode(" ", $time_string);
unset($mail_date[6]);
$mail_datetime = date_create_from_format('D, j M Y G:i:s O', implode(" ", $mail_date));
$time_diff_in_seconds = time() - $mail_datetime->getTimestamp();

Time zone confusion processing javascript generated date in php

I am located in the (PDT) time zone at time Sat May 11 2013 20:58:51 (my time) I generated a date/time using the following code.
var date = new Date();
alert(date);
This returns the result
"Sat May 11 2013 20:58:51 GMT-0700 (PDT)"
If I then post this date to a php script which processes it in the following way:
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
I get the result
"2013-05-12 03:58:51"
Not what I expected. I only get the expected time if I get rid of the "GMT-0700 (PDT)" part from my date/time. So I have two questions.
Can anyone tell me how to generate a date of this format but with out the "GMT-0700 (PDT)" part in javascript without using string functions/regex?
Is my browser giving me the wrong time zone, or is php interpreting the time zone incorrectly. In either case, why?
JavaScript always take your local server timezone, while PHP is converting your date to UTC, so you need to set your server time zone before using strtotime as follow,
date_default_timezone_set('America/Los_Angeles');
$date = date('Y-m-d H:i:s', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
echo json_encode($date);
DEMO.
Or if you would like to convert your js date to UTC than use,
var date = new Date();
var utcdate = date.toUTCString()
$date = date('r', strtotime("Sat May 11 2013 20:58:51 GMT-0700 (PDT)"));
1- echo date("l F j, Y, H:i s");
2-
date_default_timezone_set('America/Los_Angeles');
$postedDate = "Sat May 11 2013 20:36:24 GMT-0700 (PDT)";
$date = date('Y-m-d H:i:s ', strtotime($postedDate));
echo json_encode($date);

How to add an hour onto the time of this datetime string?

Here's an example of the datetime strings I am working with:
Tue May 15 10:14:30 +0000 2012
Here is my attempt to add an hour onto it:
$time = 'Tue May 15 10:14:30 +0000 2012';
$dt = new DateTime($time);
$dt->add(new DateInterval('P1h'));
But the second line gives the error that it couldn't be converted.
Thanks.
You should add a T before the time specification part:
$time = 'Tue May 15 10:14:30 +0000 2012';
$dt = new DateTime($time);
$dt->add(new DateInterval('PT1H'));
See the DateInterval constructor documentation:
The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.
(Emphasis added)
Previous answers works fine. However, I usually use datetime modify in my externally hosted websites. Check php manual for more information.
With the code proposed, it should work like this:
$time = 'Tue May 15 10:14:30 +0000 2012';
$dt = new DateTime($time);
$dt->modify('+ 1 hour');
For those not using object orientation, just use it this way (first line DateTime just to bring somethng new to this thread, I use it to check server time):
$dt = new DateTime("#".$_SERVER['REQUEST_TIME']); // convert UNIX epoch to PHP DateTime
$dt = date_modify($dt, "+1 hour");
Best,
Using strtotime():
$time = 'Tue May 15 10:14:30 +0000 2012';
$time = strtotime($time) + 3600; // Add 1 hour
$time = date('D M j G:i:s O Y', $time); // Back to string
echo $time;

Converting between timezones in PHP

I am converting this time and date:
Thu, 31 Mar 2011 02:05:59 GMT
To the following time and date format:
Monday March 28 2011 4:48:02 PM
I am using the following PHP code to accomplish this, but I want to convert all time zones to PST/PDT. I looked at the PHP manual and saw this date_default_timezone_set() but I am not sure how to implement that into the code I have below.
$date = $messages[0]->CreationTime;
echo date('l F j Y g:i:s A I', strtotime($date))
I would not use date_default_timezone_set for general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)
Instead, I would use something like:
$tz = new DateTimeZone('America/Los_Angeles');
$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimezone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";
$date = $messages[0]->CreationTime;
date_default_timezone_set('America/Los_Angeles');
echo date('l F j Y g:i:s A I', strtotime($date));
See this list for available timezones that get passed into the function

Using PHP to insert/output dates and time in mysql?

I don't know why it is so confusing, maybe it s because there are so many ways to output/input date and time with mysql and php. All I want is for users to put a date in this format
MM/DD/YYYY
and for it to output in this format
Month Name - Date
and for time, I want users to pick the time from a select field, starting with 1:00 P.M. and incrementing down 30 minutes, so it will be 1:30 P.M., 2:00 P.M... and I want to somehow insert this into MySQL and output it as 1:30 P.M. Not sure how to do that either.
The best way to store a date format in a database and be able to display it the way you want is to use the time() and date() function.
when storing the date in your database you should use time(), it will generate a string like this -> 1300695900. this little string contains the date and time
then you can use date to display it in any way you want:
$time = time();//you would normally get this from the database
date('d M Y', $time); //outputs 21 Mar 2011
date('m-d-y', $time); // outpus 03-21-2011
and so on...
Edit:
to answer your last question, you just get the different values, stick it together (concatenation) and then use strtotime()
$date = $_POST['date']; // eg 03/03/2011
$time = $_POST['time']; // eg 1:30
$daypart = $_POST['daypart']; // eg PM
$date_time = $date.' '.$time.' '.$daypart;
$strtime = strtotime($date_time);
echo date('d M Y - h:i', $strtime);// outputs 03 Mar 2011 - 01:30

Categories