Issue with displaying time using gmstrftime - php

when i am trying to print 160000 in gmstrftime.it results 20:26 but for 80000 it gives 22:13. i am not sure why this happening.Is their any kind of limit for gmstrftime?
echo gmstrftime('%H:%M', 160000);
it gives 20:26.
echo gmstrftime('%H:%M', 80000);
it gives 22:13

Just as deceze pointed out the second arg is the epoch or unix time. Number of seconds since 1970 January 1 00:00. If you leave the second arg out you will get the current time.
<?php
$time1 = strtotime("2014-02-03 20:26");
echo "First time (".date('c', $time1).") in epoch: $time1\n";
echo gmstrftime('%H:%M', $time1)."\n";
$time2 = strtotime("2014-02-03 22:13");
echo "Second time (".date('c', $time2).") in epoch: $time2\n";
echo gmstrftime('%H:%M', $time2)."\n\n";
?>
Plug in different dates for $time1 and $time2 to see the epoch time.

160000 is not a timestamp in any way that PHP (or other common systems) define a timestamp. What you have is merely a time of day expressed in "military time" stored as an integer. So first of all, any gm function is likely not what you want to use, since you are missing timezone information from this "timestamp" to begin with, hence conversion to GM time is ambiguous if not impossible.
The best you may be able to do is:
$timestamp = DateTime::createFromFormat('His', sprintf('%06d', 160000))->format('U');
echo strftime('%H:%M', $timestamp);

Related

PHP date() is wrong when using a UTC timestamp

$dayBasedOnUTC = date('l', $_GET['day']);
Why is it that when I echo the value of $dayBasedOnUTC the day returned is a Tuesday?
The UTC value of $_GET['day'] is: 1409393126144
If you put that number into any Unix Timestamp Converter you will see the date is Saturday.
It appears that 1409393126144 is a Javascript timestamp, which is counted in milliseconds. PHP expects its UNIX timestamps in seconds though. So 1409393126144 to PHP is a timestamp in the far future.
Divide by 1000 to get the correct value:
echo date('l', 1409393126144 / 1000);

Is PHP strtotime function when given format of only m/d/Y guaranteed to return first second of that day?

strtotime() returns number of seconds since so and so date. OK. So it's all in seconds. Now, if you give a date format which consists of only day, month and year, what time does it return in terms of seconds. The very first second of the day, the last second or undefined in between? The manual does not provide any guidance and common sense would assume the first second. Why is this significant? It could be when comparing or computing time interval between a fully defined date and a partially defined datetime (one without hours, minutes and seconds).
strtotime("1/1/2014")
Is this "guaranteed," as opposed to expected, to return the very first second of the new year?
It will return the time from 00:00:00, e.g. strtotime("1/1/2014"); = strtotime("1/1/2014 00:00:00");
In case you need to be sure, just use:
strtotime("1/1/2014 00:00:00");
Yes, it will always return first second of that day:
echo date('Y-m-d H:i:s', strtotime("1/1/2014"));
# 2014-01-01 00:00:00
demo
but to be sure, just enforce time like #Pekka suggested:
echo strtotime("1/1/2014 00:00:00");

strtotime() returns different Unix timestamp in different timezones for string "now"

php > echo strtotime("now America/New_York")."\n";
1376459035 // -> 2013-08-14 05:43:55 - Wrong
php > echo strtotime("now UTC")."\n";
1376444635 // -> 2013-08-14 01:43:55 - OK
php > echo time()."\n";
1376444635 // -> 2013-08-14 01:43:55 - OK
Can anyone explain?
Is this some PHP's invention – timezone-"corrected" unix timestamps?
// edit:
I realize it makes no sense to even specify the timezone with "now". It does, however, with other relative times, e.g. "tomorrow midnight". There, depending on the timezone, "tomorrow" could be a day further away, depending on whether the timezone is over midnight already. The behavior is equally weird, just a bit harder to explain.
As explained on Unix time wiki, Unix Epoch is always in UTC. Thats why outputs from
echo strtotime("now UTC");
echo time();
are the same. According this info New York time zone is UTC -5 hours. With current daylight saving time +1 now it equally UTC -4 hours. That's why you got result 2013-08-14 05:43:55 (4 hours diff).
you are using
strtotime("now America/New_York") thats why it is showing unix timestamp in different timezone for string.
use this:
date_default_timezone_set("America/New_York");

Does date_default_timezone_set() effects time()?

If I set a time zone like:
date_default_timezone_set('America/Los_Angeles');
Will it affect the value of time()?
No, it does not. Your system knows the absolute time. Timestamps (time()) represent absolute timestamps. Timezone settings only affect human readable time formats as produced by date(), since those are relative to a specific location on earth (because 12:00 noon it typically when the sun is highest in the sky, and that varies between locations).
I don't believe so
Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Note the GMT on the end.
Also read the first comment on the php.net for time()
The documentation should have this info. The function time() returns
always timestamp that is timezone independent (=UTC).
<?php
date_default_timezone_set("UTC");
echo "UTC:".time();
echo "<br>";
date_default_timezone_set("Europe/Helsinki");
echo "Europe/Helsinki:".time();
echo "<br>";
?>
Local time as string can be get by strftime() and local timestamp (if ever needed) by mktime().

understanding gmtime return value

I can't understand why gmdate() and date() reutrn the same values if my server is not configured to be on Greenwich Mean Time.
Why is this?
echo time(); // 1311011114
echo date("U"); // 1311011114
echo gmdate("U"); // 1311011114
echo date("j-m-y H:m:s"); // 18-07-11 12:07:14
echo date("e"); // America/Chicago
echo date("O"); // -0500
echo date("T"); // CDT
UPDATE
how do I obtain current time on Greenwich? calculating with date("O")? is there other way?
Because time never changes -- it is always seconds since epoch (GMT).
Time is always the same. It is just your time-zone is different and that is how display date differs.
You can change your timezone in order to see time in the different zones.
See here for all the Date/Time functions
So a long time ago, people needed a way to determine time across multiple computing systems that was uniformed. The Computing Syndicate Council of Wise Elders (CSCWE for short) of the pre-Internet age were the ones that decided upon this. A secret ballot decided the arbitrary beginning point would be 1970 to herald a more advance computing age. So from that moment onwards, an unending march of seconds through the decades began, a new time for computing revolution with the steady beat of the seconds.
From this arbitrarily defined beginning, all computing time can be determined by running mathematical wizardry against this ever increasing number of seconds and then factoring in the timezones.
It is already answered but if you want to get local time and UTC time;
date_default_timezone_set('America/Chicago');
$format = 'Y-m-d H:i:s';
$time1 = time();
$time2 = strtotime(gmdate($format));
print date($format, $time1);
print date($format, $time2);
// 2014-07-24 17:31:23
// 2014-07-24 22:31:23

Categories