PHP - gmdate() and Large Integers

coffeemonitor

I'm getting a result I'm not expecting from gmdate()

<?php
$secs = 175707;
echo gmdate("H:i:s", $secs); // result: 00:48:27
?>

The result is 00:48:27, which is way off. It appears the hours is getting pushed down a position. Am I suspecting that right?

Corbin

gmdate works on dates, not periods of time. In other words, your timestamp is being interpreted as a point in time early in January 3rd of 1970 (specifically 00:00:00 1 Jan 1970 + 15707 seconds = 00:48:27 3 Jan 1970). This is where your 00:48:27 comes from.


gmdate (and date) are not meant to be used this way. If you just want to calculate hours/minutes/seconds based on number of seconds, calculate them directly:

$seconds = 175707;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;

echo "$hours:$minutes:$seconds"; //48:48:27

Make note that this does not work with civil days. This is because of daylight saving time. A day is not always 24 hours. Sometimes it's 23, and sometimes its 25 when DST is coming into effect or ending. With days as a unit of measure (i.e. a day is always 24 hours), this does work. As an example, 10 March is 23 hours and 3 Nov is 25 hours in the United States. If you are happy with static 24 hours days, then the same approach will of course work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

php gmdate() Incorrect time zone

From Dev

PHP randomly decrements large integers by 1

From Dev

php, normal date is correct, but gmdate is not accurate

From Dev

Store a value in MySQL made from gmdate() in PHP

From Dev

php, normal date is correct, but gmdate is not accurate

From Dev

What are the maximum limit of bitwise operation on large integers in PHP?

From Dev

when i add 2 hours in gmdate it will zero minutes in Php

From Dev

3rd Last Day of the Month using PHP gmdate()

From Dev

Using strtotime() PHP and revert back trough gmdate() is not returning same date

From Dev

AWK sum of large integers

From Dev

Handling large integers in python

From Dev

Parallel arithmetic on large integers

From Dev

Python 3 with large integers

From Dev

Handling large integers in python

From Dev

Distinguishing large integers from near integers in python

From Dev

merge sort with large number of integers

From Dev

The Sum of large Integers using Arrays

From Dev

Different results for date() and gmdate()

From Java

Measure the efficiency of appending integers to a large dictionary of lists

From Dev

How does python represent such large integers?

From Dev

Vector size() returning seemingly random large integers

From Dev

Handling large integers in Java without using BigInteger

From Dev

Bit manipulation on large integers out of 'int' range

From Dev

Python extension - construct and inspect large integers efficiently

From Dev

Handling large integers in Java without using BigInteger

From Dev

C atmega2560 Division of large integers

From Dev

Large integers addition error in python 2.7.10

From Dev

large integers in cypher, neo4j

From Dev

Comparing two integers in php

Related Related

HotTag

Archive