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);
Related
I'm using Highcharts and need the dates formatted like this:
UTC.(2016,11,01) // for December 1st 2016
Is there an easy way to do this? Because
date('Y-m-d', strtotime('-1 month'))
gives me the wrong output for January.
You don't need sub the month. Just use other format as ATOM/RFC3339. It's fully supported by javascript Date class
$date = new DateTime('2016-01-01');
echo $date->format(DateTime::ATOM);
Result:
2016-01-01T00:00:00-05:00
Javascript:
var date = new Date('2016-01-01T00:00:00-05:00');
console.log(date)
As I'm live in Brazil, my browser show as
Fri Jan 01 2016 03:00:00 GMT-0200 (Horário brasileiro de verão)
If you have any other format as input, you could use DateTime::createFromFormat and the rest still the same
$date = DateTime::createFromFormat('m/d/Y' , '12/01/2016');
I have an old MySQL database. Here is a time column.
I see here is some time values Like:
2013-06-03 21:33:15
So, I want to convert this time to my local time UTC +6 in my PHP Script.
How can it possible?
I can make a mysql query to get the time from Database to my my PHP variable $TimeFromMySQL
Just now I want to show like:
11:32:44 PM 05 July 2014
Thank You
See VMai's comment above if you want to do this in MySQL. For PHP:
$inDate = '2013-06-03 21:33:15';
$inDate_tz = 'America/Chicago';
$original_date = new DateTime($inDate, new DateTimeZone($inDate_tz) );
$original_date->setTimeZone(new DateTimeZone('Asia/Dhaka'));
$new_date = $original_date->format('H:i:s d F Y');
echo $new_date;
//outputs 08:33:15 04 June 2013
My answer here might be too late, still it might be helpful for some one who run to the same situation like me before I work around this solution.
To convert datetime to UTC that is, getting correct location Time and Date.
I came up with this:
// Get time Zone.
$whereTimeNow = date_default_timezone_get();
// Set the time zone.
date_default_timezone_set($whereTimeNow);
$dateTime = date('d-m-Y H:i');
$dateTimeConvert = new DateTime($dateTime, new DateTimeZone($whereTimeNow) );
$dateTimeConvert->setTimeZone(new DateTimeZone('UTC'));
$dateTimeNow = $dateTimeConvert->format('Y-m-d H:i:s');
echo $dateTimeNow;
my server is on GMT -4 and all the records which go into the database are GMT -4 using
<?php date("Y-m-d H:i:s");?>
so if the time was 15:00:00 GMT +1 (my current timezone)
the time added to the database would be 10:00:00.
however when I use javascript new Date(); it gives it as
Thu Aug 29 2013 22:17:00 GMT+0100 (GMT Summer Time)
i have this function to convert the js date to php
$jsDate = $_REQUEST["date"];
$jsDateTS = strtotime($jsDate);
if ($jsDateTS !== false)
{
$jsDateTS = date('Y-m-d H:i:s', $jsDateTS );
echo $jsDateTS."<br>";
but it writes 2013-08-29 22:17:00 and not 2013-08-28 17:17:00 which is what I need.
how do I convert my javascript new Date to GMT -4? is it possible or not?
thanks, and sorry if I am unclear in any way.
my solution was the following
client side
var date = new Date();
date = date.toUTCString();
server side
$date = date('Y-m-d H:i:s', strtotime($_REQUEST["date"]) );
thanks for all others who answered
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. :-)
I have the following timestamp:
1342259667654
which when converted with http://www.epochconverter.com/ gives:
Assuming that this timestamp is in milliseconds:
GMT: Sat, 14 Jul 2012 09:54:27 GMT
Your time zone: 14. juli 2012 11:54:27 GMT+2
And that is the correct time, but when using:
echo date("Y-m-d H:i:s", 1342259667654);
I get the following date:
1904-07-24 10:22:47
How can I get with PHP the exact date out of this time stamp?
Your timestamp needs to be divided by 1000:
echo date("Y-m-d H:i:s", 1342259667654/1000);
$timestamp = 1342259667;
$dt = new DateTime("#$timestamp"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
You can also do it this way.
The value 1342259667654 is actually in miliseconds, while PHP's date() function is unable to handle miliseconds value. Hence the weird output.