javascript new Date() set to GMT -4 - php

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

Related

How to convert any time zone into UTC using Laravel

I want to convert the user's time, eg. 08:45 P.M, to UTC time zone. How can I do that?
if ($request->open_at)
{
$time = Carbon::parse($request->open_at)->toUTCString();
dd($time);
$query->whereTime('open_at', '>=', $time);
}
Like such, but unless you're always starting from the system timezone (configured in PHP), date must already have the correct timezone set for this to work, like others have mentioned.
$time = Carbon::parse($request->open_at);
$time->setTimezone('UTC');
...
Carbon extends the DateTime object including setTimezone
Use this PHP approach:
$time = new DateTime("08:45 P.M");
$time ->setTimezone(new DateTimeZone("UTC"));
echo $time ->format("Y-m-d H:i:s e");
User's time zone can be fed into the optional second argument of parse(). I've also been unable to find any toUTCString() method (🤔). All together:
$userTimeZone = 'Europe/Berlin'; // We'll come to this later
$time = Carbon::parse($request->open_at, $userTimeZone)->setTimezone('UTC');
echo $time->format('r');
For example:
foreach (['Asia/Tokyo', 'Europe/Berlin', 'America/Los_Angeles'] as $userTimeZone) {
echo "$userTimeZone\n";
$time = Carbon::parse('08:45 P.M', $userTimeZone);
echo $time->format('r'), "\n";
$time->setTimezone('UTC');
echo $time->format('r'), "\n\n";
}
Asia/Tokyo
Fri, 24 Dec 2021 20:45:00 +0900
Fri, 24 Dec 2021 11:45:00 +0000
Europe/Berlin
Fri, 24 Dec 2021 20:45:00 +0100
Fri, 24 Dec 2021 19:45:00 +0000
America/Los_Angeles
Thu, 23 Dec 2021 20:45:00 -0800
Fri, 24 Dec 2021 04:45:00 +0000
Of course, all of this is pointless if you don't know the user's time zone.
Do you have such information? The easiest way is to probably just ask, although you can also use some tricks to try and guess, such as:
Client-side JavaScript
Geolocation APIs
Carbon has ->utc() method (equivalent to setTimezone('UTC')) and Laravel query build can take Carbon object without having to format it as a string:
$query->whereTime('open_at', '>=', Carbon::parse($request->open_at)->utc());
Use the set timezone function to convert time
$time = Carbon::parse($request->open_at);
$time->setTimezone('UTC');

PHP - Convert string to date in

I am getting a string like Wed Sep 28 2016 01:00:00 GMT+0500 (PKT) and I need to convert it to 2016-09-28 01:00:00
I have tried this
$startTime = strtotime($updatedData['start']);
echo $time = date("Y-m-d H:i:s",$startTime);
but it returns me 2016-09-27 20:00:00
You could change it to use DateTime:
$startTime = new DateTime('Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)');
echo $startTime->format('Y-m-d H:i:s');
DateTime keeps the timezone you give him.
Live Example: https://3v4l.org/UTltO
#copynpaste solution is nice and straight forward but I will still share my solution by using Carbon.
Carbon is a library included together with laravel and here is the
documentation.
$carbon = new Carbon('Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)');
$carbon->format('Y-m-d H:i:s');
echo $carbon;
it will come out the result same as DateTime
2016-09-28 01:00:00
So what carbon nice is you can just add day, minute, second and etc by just a very minimal code, here is an example:
$carbon->addDays(1);
echo $carbon;
//result
2016-09-29 01:00:00
Try this:
$startTime = strtotime($updatedData['start']);
$time = date("Y-m-d H:i:s",$startTime);
echo date( "Y-M-d H:i:s", strtotime( $time) + 5 * 3600 );
It returns UTC time and you do need to add 5 hours to it. Also a quick suggestion. You can use Carbon for handling the date time.
You can set the desired time zone before converting. Please see the below code as a reference.
date_default_timezone_set("Asia/Bangkok");
$str = 'Wed Sep 28 2016 01:00:00 GMT+0500 (PKT)';
$startTime = strtotime($str);
echo $time = date("Y-m-d H:i:s",$startTime);

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);

Javascript time to PHP time

I have a time value in javascript for example 11:30:00.
Date {Mon Oct 22 2012 11:30:00 GMT+0800 (Taipei Standard Time)}
and I passed to a php file and converted it by:
$newTime = date('H:i:s', $theTime);
But, it return 05:36:00. What is the right way of concerting time?
Use myDate.getTime() instead, and then divide this by 1000 since PHP deals with seconds while JavaScript deals with milliseconds.
If you're looking to use PHP to parse the date/datetime, then you should use strtotime(). Something like:
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));
Which would output:
2012-10-22 04:30:00
This output is GMT, which you can change if required.
EDIT:
If you're expecting 11:30:00, then try the following:
date_default_timezone_set('UTC');
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));

Linux timestamp to PHP

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.

Categories