timestamp in PHP and Javascript - php

I have this timestamp how to convert it to unix timestamp
1.6749018E+12
Or is there any way to decode it in PHP let me know.
have good day!
My code
$t = '1.6749018E+12';
print date('H:i:s', $t);
Not work!

1.6749018E+12 looks like a unix time with milliseconds as float number. If I'm right, then the conversion to a unix time in seconds is:
$unixtime = intval($NUMBER/1000)
where $NUMBER is the input number (1.6749018E+12 in you example). This number represents: Sat Jan 28 11:30:00 CET 2023 (or 10:30 UTC).

Related

Convert Date to Hours only using PHP

I would to add a certain amount of hours to a date time using PHP.
I am interested to show the result only using hour format.
For example, I add 8 hours to a date time as follow:
$result= date("H:i", strtotime('18:00') + 8*3600);
But I got as result value 01:00, but I would get 26 as result..
Can you help me please, I could not find the solution :(
Thank you all
It's easier with DateTime:
$date = new DateTime('18:00:00');
$date->modify('+8 hours');
echo $date->format('H:i');
Edit: Maybe I don't understand the question then. If you want to get 26, then you can use something like:
$date = new DateTime('18:00:00');
echo $date->format('H') + 8;
Basic PHP dates math. strtotime() returns a unix timestamp, which is number of seconds since the epoch, midnight January 1,1970.
date() takes those timestamps and formats them into whatever representation you want. But 'H' is NOT "hours since time zero". it's "hours of the day". If you have a timestamp that represents Jul 8/2014 12 noon, then H is going to be 12, because it's noon. It's not going to be 50 bajillion hours since Jan 1/1970.
e.g.
Jul 8/2014 11pm + 3 hours = Jul 9/2014 2am.
date('H' of Jul 9/2014) = 2, not "14"

How to make PHP date() ignore local GMT setting?

I am storing the time of day as a number of seconds since midnight. I have a number that should be 8:00 am:
//3600 secs / hour * 8 = 28800
$time = 28800;
var_dump(date('h:i a', $time ));
My output is:
string(8) "01:00 am"
Based on my location, I am -7:00 GMT, so I can see where I would get 1:00 am, but how do I do format this time to show 8:00 am, essentially making it ignore the current GMT setting while formatting this time?
two ways.
first you may try gmdate() function which output the raw GMT time .
and the other way you can set timezone before you use date function.
as follow .
date_default_timezone_set('Asia/Shanghai');
echo date('H:i:m', time());
I figured this out. The solution is to use gmdate(). It will format a raw timestamp to GMT.

PHP strtotime EST back and forth conversion

For EST time, I've set:
date_default_timezone_set("America/New_York");
As the page loads, I'm getting EST time via:
$time = time();
The problem is when I convert strings back and forth between timestamps and datetime format:
10/31/2012 7:30pm 1351729800 | 10/31/2012, 8:30 pm
11/2/2012 7:30pm 1351902600 | 11/02/2012, 8:30 pm
11/3/2012 8:00pm 1351990800 | 11/03/2012, 9:00 pm
11/7/2012 8:00pm 1352336400 | 11/07/2012, 8:00 pm
11/9/2012 8:00pm 1352509200 | 11/09/2012, 8:00 pm
11/10/2012 8:00pm 1352595600 | 11/10/2012, 8:00 pm
I'm expecting these date & times to be the same.
The first section (before the "|") is simply strings, such as "10/31/2012 7:30pm" and the strtotime("10/31/2012 7:30pm EST").
The section (after the "|") is date() of the previous strtotime() value.
What can I do to convert from string to time (strtotime) and double check that the date format returned is the same as the string input?
Thanks
I'd say that this is because currently New York is on daylight savings time - EDT rather than EST. This affects things like so:
date_default_timezone_set("America/New_York");
strtotime("10/31/2012 7:30pm"); // translates to "Wed, 31 Oct 2012 19:30:00 -0400"
strtotime("10/31/2012 7:30pm EDT"); // translates to "Wed, 31 Oct 2012 19:30:00 -0400"
strtotime("10/31/2012 7:30pm EST"); // translates to "Wed, 31 Oct 2012 20:30:00 -0400"
The quick fix is probably to not add the timezone to the string, strtotime() will use the correct default timezone you set.
You can be a bit more exact about how your date is being parsed by using the DateTime createFromFormat function:
$date = DateTime::createFromFormat('m/d/Y g:ia', "10/31/2012 7:30pm");
echo $date->format('U');
Alternatively if you wait until November the problem will resolve itself :-)
Some times you need to set and forget a time in a PHP script and moving to a new server could change your hard work. if you are doing date with string to time for example add your time zone code to your string like New Yourk wiht daylight savings time: 'EDT'
echo date('g:i a',strtotime('10:59 EDT'));// 10:59 am
or if you want to extract the amount of seconds an specific time has, lets say 10:59pm, use something like:
echo gmdate('g:i a',strtotime('January 1 1970 10:59pm GMT'));//82740 seconds
//converted to 10:59pm
the latter will give you (12+10 hours(*60) and 59 minutes)*60 in seconds, and the gmdate function will echo the GMT time from what ever seconds you pass to the function.. if you try date instead you will, most likely, not get 10:59 unless you live in the GMT time zone (Greenwich Mean Time).
This time zone stuff can get very confusing, so be very careful and mess around with it when having a very specific plan..like converting someones time to yours, etc.

Why don't PHP and Javascript's timestamps match?

If I do
alert(new Date(1313690400000))
returns: Thu Aug 18 2011 13:00:00 GMT-0500 (CDT)
however, PHP
echo date('Y-m-d H:i:s', 1313690400000);
returns: 1951-12-14 05:50:24
JavaScript uses milliseconds as a timestamp, whereas PHP uses seconds. As a result, you get very different dates, as it is off by a factor 1000.
So remove three zeroes at the PHP side:
echo date('Y-m-d H:i:s', 1313690400);
Javascript Date is milliseconds since Epoch, whereas PHP date uses unix timestamp which is in seconds.
So to get the same date in php, divide by 1000 first
PHP's date/time functions use the number of seconds since the epoch, while Javascript uses the number of milliseconds. In your php func:
echo date('Y-m-d', 1313690400000 / 1000);

PHP: Compare file timestamp with system time

I am new to PHP and am writing a PHP script that contains a module which reads the file timestamp out of a file and then compare with system time to see whether the file is older than 5 minutes or not and I am wondering how that can be achieved. I am currently using
$timeStamp = strftime('%c', filectime($this->localPath));
$timeStamp2 = filectime($this->localPath);
The two $timeStamp and $timeStamp2 are different, the 1st one is more human readable
$timeStamp Mon Jun 20 15:17:01 2011
$timeStamp2 1308608221
What does $timeStamp2 mean?
And again, how to see if the file is more than 5 minutes old?
That is Unix timestamp actually (seconds since 1st jan 1970 or EPOCH)
You can use time() function to get current time in same Unix format.
And then subtract both time values to check whether difference is > 300 (5 min) or not.
$timeStamp2 is a UNIX timestamp (the number of seconds passed since 01/01/1970).
You can get the same thing from $timeStamp1 by doing
$timeStamp1 = strtotime($timeStamp1)
and then compare the two values
Answering your question "What does $timeStamp2 mean?"
1308608221 is the number of seconds that have passed since midnight January 1,1970.

Categories