Linux timestamp to PHP - 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.

Related

php date return 1970-01-01 08:00:00

<?php
echo date("Y-m-d H:i:s", 0);
?>
//Result: 1970-01-01 08:00:00
My question: The second parameter of function date is 0, It should be 1970-01-01 00:00:00.
Why it is 1970-01-01 08:00:00 ?
I set config in php.ini
[Date]
date.timezone=Asia/Ho_Chi_Minh
//Asia/Ho_Chi_Minh: UTC + 7 (GMT + 7)
Please help me.
From the wiki:
The Unix epoch is the time 00:00:00 UTC on 1 January 1970
As you see, the beginning is started by the UTC timezone. That means that it was 8:00 in Asia/Ho_Chi_Minh when the Unix epoch started.
If you need to get 0:00 you have to set negative second parameter
date_default_timezone_set('Asia/Ho_Chi_Minh'); // Set UTC timezone
echo date("Y-m-d H:i:s", -(8 * 60 * 60));
Demo
Date php and Default timezone set if you want to set your current time:
date_default_timezone_set('Asia/Ho_Chi_Minh');
echo date("Y-m-d H:i:s");
you don't need the second parameter.
date() always shows the local time. That’s for you Asia/Ho_Chi_Minh. The gmdate() function is used to display the UTC/GMT time.
echo gmdate("Y-m-d H:i:s", 0);
//Result: 1970-01-01 00:00:00
You can also use DateTime. If DateTime is created from a timestamp, the internal time zone is always UTC.
$dateTime = new DateTime('#0');
echo $dateTime->format("Y-m-d H:i:s");
//Result: 1970-01-01 00:00:00
You don't need to change your default timezone for either option.

How to convert GMT time to IST?

Here I am getting GMT Time as follow.
1520489880
Now I am getting This as
$myfinaldate = gmdate("d M H:i", $strtoDate);
So it will returns me GMT time. Now i want to convet it to IST(indian standard time).
where I stuck when I try
//Set timezone to india
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s');
But this returns me current time not the time that I get in strtotime
You need to pass the timestamp:
date_default_timezone_set('Asia/Kolkata');
echo date('d M H:i', $strToDate); // where $strToDate is 1520489880
Would produce the following value:
08 Mar 11:48
You can convert GMT time to IST time using following ways :
1.
echo date('Y-m-d H:i:s',strtotime('+330 minutes', 0));
2.
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s');
Changing default timezone is not such a good idea. Use DateTime class, any you don't need to worry about default timezone anymore...
$dt = new DateTime("#$strToDate");
$dt->setTimezone(new DateTimezone('Asia/Kolkata'));
echo $dt->format('Y-m-d H:i:s');
demo

Converting Date format in PHP

I am getting a date format like Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time) form a javascript to my php function.
I want to store that to my database table in "2013-04-22 12:16:00" format.
Can any one help me to convert this date type.
I tried:
date("Y-m-d H:i:s",$startDate);
But it's giving error as
date() expects parameter 2 to be long string given
Use strtotime() and date():
$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
or use DateTime:
<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>
DEMO
Try using strtotime to convert the timestamp to unix-format so you can use it in the date() function:
date("Y-m-d H:i:s",strtotime($startDate));
You can also try using the (usually included) DateTime class. In particular, have a look at DateTime::createFromFormat. It may help you get around ambiguities in the date string (strtotime() will sometimes fail or mis-parse a date string). DateTime::createFromFormat allows you to specifically designate the format of the date string so there can be no ambiguity.
firstly you need to save it as Timestamp so,
$startDate = strtotime($javascript_value);
$result = date("Y-m-d H:i:s",$startDate);

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

convert any date string to timestamp without timezone

I'm getting xml and rss feeds and putting the data into a database. I've run into two different date formats so far...
Wed, 21 Jul 2010 00:28:50 GMT
And
2010-07-20T17:33:19Z
I'm sure there will be more. My postgresql database for the date is timestamp without time zone. Is there an existing function in php or is there a procedure to convert the any date strings to timestamp without time zone (Y-m-d H:i:s)?
Use date with strtotime:
$date = date('Y-m-d H:i:s', strtotime('Wed, 21 Jul 2010 00:28:50 GMT'));
echo $date;
Result:
2010-07-21 05:28:50
.
$date = date('Y-m-d H:i:s', strtotime('2010-07-20T17:33:19Z'));
echo $date;
Result:
2010-07-20 22:33:19
You don't need to convert it at all. PostgreSQL should convert automatically:
postgres=# create table test_tz (f1 timestamp without time zone);
CREATE TABLE
postgres=# insert into test_tz (f1) values ('Wed, 21 Jul 2010 00:28:50 GMT');
INSERT 0 1
postgres=# insert into test_tz (f1) values ('2010-07-20T17:33:19Z');
INSERT 0 1
postgres=# select f1 from test_tz;
f1
---------------------
2010-07-21 00:28:50
2010-07-20 17:33:19
Timestamps are considered to be UTC.
$dt = new DateTime('Wed, 21 Jul 2010 00:28:50 GMT');
echo $dt->format('U'); // 1279672130
is the same timestamp as
$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 CEST');
echo $dt->format('U'); // 1279672130
Note that the U formatting option requires PHP5.3 though. When supplying a timezone identifier in the Date String, the DateTime object recognizes the Timezone, so when you call the following on the GMT DateTime instance
echo $dt->format('Y-m-d H:i:s');
it will return 2010-07-21 00:28:50. You can change a DateTime object's timezone with it's setTimezone() method though.
$dt = new DateTime('Wed, 21 Jul 2010 02:28:50 GMT+2');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s'); // 2010-07-21 00:28:50
But if you just need the timestamp, it's not needed.

Categories