PHP - gmdate() and Large Integers - php

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?

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.

gmdate() expects a UNIX timestamp - seconds since Jan 1/1970. You've passed in the equivalent of Saturday, Jan 3rd, 1970, 12:48:27am.
e.g. try this:
echo gmdate('r', 175707);

From the manual
the time returned is Greenwich Mean Time (GMT)
So if you're in CET (GMT +1) it indeed will give you an hour of 0.

Have a look at this if you want to see where you are going wrong:
echo date("d-M-Y H:i:s", 175707);
That will give an output of 03-Jan-1970 00:48:27. i.e. 175707 seconds from 00:00:00 on the 1st of January 1970 = 03-Jan-1970 00:48:27.

Related

How to convert date to hours

I am having :
hours1 : 403440
and date2 as :2016/01/10
I need to convert date2 in hours and find the difference between the two which should again be in hours.
The second parameter of date is a unix timestamp which is in seconds so multiple your hours by 3600 (3600 seconds per hour).
$hours1 = 403440 * 3600;
$date1 = date("d-m-Y H:i:s", $hours1);
echo $date1;
Output:
09-01-2016 19:00:00
Per your update your code should be:
$date2 = strtotime('2016/01/10');//get date to seconds from 1970
$hours1 = 403440 * 3600; // convert from hours to seconds
echo ($date2 - $hours1)/3600;//subtract seconds then divide by 3600 to get how many hours difference is
Output:
5
The second parameter of date() is unix timestamp which is the number of seconds since January 1 1970 00:00:00 UTC.
So, if $hours1 is number of hours since January 1 1970 00:00:00 UTC, it should be
date("d-m-Y H:i:s",$hours1*3600);
to yield the correct date string.
On the other hand, $hours2=(strtotime($date2)/3600); is giving you the correct number of hours since January 1 1970 00:00:00 UTC.

Bizzare PHP behaviour calculating time differences, only on 6th and 7th September, 2013

I'm trying to calculate the difference in days between two dates. I'm getting bizzare behaviour - I've narrowed it down to 6th and 7th October, 2013, as you can see below. Whenever the date range spans those dates, the calculation is a day out.
// WRONG! current year - 2013
$datediff = strtotime('2013-10-07') - strtotime('2013-10-06');
$startToEndDays = floor($datediff/(60*60*24));
print_r($startToEndDays); // Outputs 0 - should output 1
// RIGHT! next year - 2014
$datediff = strtotime('2014-10-07') - strtotime('2014-10-06');
$startToEndDays = floor($datediff/(60*60*24));
print_r($startToEndDays); // Outputs 1 - correct
Any idea what could be the issue here?
haha OK, it turns out 6th/7th October 2013 is when daylight savings starts in Sydney, Australia. So, the number of hours between those dates is calculated (correctly) as 23. But, 23 hrs is not quite a day.
If you're using PHP 5.3+, then this is how you should calculate the difference between dates in days, to save yourself any daylight savings headaches:
$startDate = new DateTime('2013-10-07');
$endDate = new DateTime('2013-10-06');
$interval = $startDate->diff($endDate);
$days = $interval->days;

How can I add two unix timestamps together?

Given that I have 2 timestamps:
1332954000
which is 18:00pm in human readable format. I got this from strtotime("18:00")
and
1330992000
which is Tues 6 march 2012 in human readable format
How can I add them together such that it will become Tuesday 6 March 2012 18:00pm in Unix timestamp format?
1332954000 doesn't mean 18:00, it means 1332954000 seconds from 1st Jan 1970.
You can't represent 18:00 in seconds from 1st Jan 1970, so your question is meaningless.
You could represent 18:00 as being equal to 18 * 60 * 60 = 64800 seconds, then add that on to your date, which would make sense.
You could concatenate the strings together then use strtotime on that to get what you want alternatively.
Solution 1
$Var1 = '18:00'; // Not 18:00pm please....
$Var2 = 'Tues 6 march 2012';
$NewTimeStamp = date('U', strtotime($Var1.' '.$Var2));
Solution 2 - Not sure
$Var1 = strtotime('18:00pm') - time();
$Var2 = strtotime('Tues 6 march 2012');
$NewTimeStamp = date('U', $Var1 + $Var2);
Be sure to go read about function.date to know how to format the time but U will give you timestamp.
Also, 18:00pm ain't really a time ... 6:00PM is or 18:00 without PM
The timestamp is just the number of seconds from the 1st of January, 1970. Assuming:
1330992000
Is the correct timestamp for Tues 6 march 2012 at 12:00 AM, then all you need to do is add 18 hours, in seconds to the timestamp.
$timestamp + 64800 = $final_timestamp

PHP Minutes in month

I need to be able to find the number of minutes passed in the current month so far. So from midnight of the first of the month until now.
How could I do this? So for example, 1AM on the first of the month would be give me 60 minutes.
Thanks
This should work for you:
$time = time();
$minutes = ($time-strtotime(date('Y-m-00', $time)))/60;
As of now $minutes === 15477.1
$seconds = time() - strtotime('2011-01-01 00:00:00');
$minutes = $seconds / 60;
To elaborate a bit more:
This is some simple manipulation of a unix timestamp (number of seconds since Jan 1, 1970). So you take the current timestamp and subtract what the timestamp would have been on the first of the month. This gives you total seconds that have elapsed this month. If you divide by 60, you get total minutes that have elapsed this month.

Beginner's question on what to add in a PHP time variable

I found this script on php.net and finds the difference between now and a future day. My question is very simple, but is a sample time or how can I make that time that it is needed for the $future_date ? Also what is the purpose of -1970 ?
Also how can I show a message when the future_date is reached or passed?
<?php
function time_difference($endtime){
$days= (date("j",$endtime)-1);
$months =(date("n",$endtime)-1);
$years =(date("Y",$endtime)-1970);
$hours =date("G",$endtime);
$mins =date("i",$endtime);
$secs =date("s",$endtime);
$diff="'day': ".$days.",'month': ".$months.",'year': ".$years.",'hour': ".$hours.",'min': ".$mins.",'sec': ".$secs;
return $diff;
}
$end_time = $future_date - time();
$difference = time_difference($end_time);
echo $difference;
//sample output
'day': 2,'month': 1,'year': 0,'hour': 2,'min': 05,'sec': 41
?>
A unix timestamp checkout the docs for time() and mktime()
You're substracting two values from each other so they need to be compatibable formats to be able to do that. Checking the documentation on time() could have saved you from this question.
date() is also a function you might want to check up on. Using date and the right parameters it will return the current year(Y) month(m) or day of the month(d) you can add and substract to these values and then pass them into mktime to get a unix timestamp like so for the current year in unix timestamp format:
$currentyear = mktime(date(Y));
Below would set $future_date to 1st Dec 2011
$future_date = mktime(0, 0, 0, 12, 1, 2011);
So Hour Min Sec goes:
$future_date = mktime(H, M, S, 12, 1, 2011);
Below would be 13:21:59 1st Dec 2011
$future_date = mktime(13, 21, 59, 12, 1, 2011);
$future_date should be a unix timestamp.
$future_date = strtotime("next week");
To check if the time has been reached
if($future_date <= time()) echo "Date reached";
$future_date would be an integer timestamp (in seconds since Jan 1, 1970) representing some time in the future.
ie:
$nextWeek = time() + (7 * 24 * 60 * 60);
Takes the current date/time and adds 7 days worth of seconds (24 hours, 60 minutes per hour, 60 seconds per minute) to get the integer time of one week from now.
Jan 1, 1970 is significant - it is called the Epoch in UNIX (January 1 1970 00:00:00 GMT) and is often used as a starting point for dates and/or computer "time" (time zero).

Categories