PhP: unix timestamp counting backwords problem - php

ok well i have this error with a counter i made witch is suppost to count down to 0
$time=1283593330+(60*15);
$time3= time();
$time2=$time-$time3;
1283593330=Sat, 04 Sep 2010 09:42:10 GMT
Error is this:
when the $time3 timestamp hit the timestamp for $time it says 05:00:00 instande of 00:00:00.
This is the code i use to call it.
Time left: '.date('g:i:s ',$time2).'<br />
im not sure if im doing something wrong, if 5 is the main time for unix_timestamp or the date commend in PHP
is there anyway to fix this? or is this from of timestamp just that bad of a idea for what i need.

Your time zone is 5 hours ahead of UTC: you say 1283593330 is 09:42, but it's actually 04:42 UTC.
When $time2 is zero, this represents the Unix time epoch: this is midnight UTC on 1st January 1970. So when you output this using date, it shows that time in your time zone: 00:00 UTC which is 05:00 in your time zone.
What's important is that $time2 is zero when the target time is reached.
Given that your counter is counting down 15 minutes, you can get the remaining time like this:
$hours = floor($time2 / 60);
$mins = $time2 % 60;
printf("Time left: %d:%02d\n", $hours, $mins);

Related

PHP get 00:00:00 of a unix timestamp

If I got a unix time which is e.g
1407050129
How do I get the 12:00AM of that unix day in unix timestamp , means the first minute of the day of that unix time.
Example if i want get today first minute
$today_first_min = strtotime("00:00:00");
Try this:
$that_day = "1407050129";
$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');
See demo
An alternate method to arrive at the same result... Unix time is a count of seconds since 1970-01-01 00:00:00 UTC, so each whole day is a multiple of (60*60*24) seconds.
Using this fact you can use the modulus operator (%) to calculate and then remove the remainder (ie. the seconds, hours and minutes) from the day, and get back to the first hours!
date_default_timezone_set('UTC');
$that_date = 1407050129;
$first_hour = $that_date - ($that_date % (60*60*24));
print date('Y-m-d H:i:s', strval($first_hour));
// 2014-08-03 00:00:00

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 calculate hours elapsed from PM to AM

Time is confusing... How can one calculate the time elapsed from PM to AM in php?
For example 22:00:00 (10pm) to 02:00:00 (02am) should give 04 hours elapsed. Instead my code returns -08 hours
function hours_elapsed()
{
$timestart = strtotime("22:00:00");
$timestop = strtotime("02:00:00");
$time_diff = $timestop - $timestart; //time difference
return gmdate("h", $total_hours);
}
It's clear that the code calculates in 24 hour format so of course it returns -08 but how is it possible to get time elapsed without 24 hour constraint.. when it passes the midnight mark?
-8 hours is 4 hours. Just add 12 if the number is negative:
$time_diff = $timestop - $timestart; //time difference
if ($time_diff < 0) {
$time_diff += 12;
}
This won't help you, however, if your dates are more than one day apart. You need to specify the date as well (as in the day of the month) to tell PHP that those times are different.
Clearly 22:00:00 is 24 hour clock, 02:00:00 could be either though couldn't it?
add the date as well, or test for the negtive and add 12 hours.
Quick solution from me: add a day to the later date.
echo gmdate('h', strtotime('1970-00-01 02:00:00') - strtotime('1970-00-00 22:00:00'));
But Blender's answer seems to be easier. :)

Timestamp intervals processed by date() don't calculate correctly

I have two unix timestamps in my database that I am subtracting to get a time interval in seconds:
$interval = $array["time2"] - $array["time1"]; // When echoed, $interval = 3
However, when I run this $interval through date(), like so:
echo date("g\h i\m", $interval);
these 3 seconds all of a sudden echo to:
7h00m
Does anyone have any idea why date() might be taking these three seconds and stretching them out into a 7 hour interval somehow?
The second argument to date() is a timestamp (seconds since midnight, Jan 1, 1970 GMT). Your interval is probably equating to 7am in your timezone relative to this date.

Removing an hour from PHP (FROM_UNIXTIME)

Could you please advice how to remove an hour from unixtime.
I have a unixtime and i need to remove an extra hour before converting to normal time. Could you please advice how to do this?
Also, is Unixtime affected by the GMT time changes?
Unix timestamps are measured in seconds, there are 3600 seconds in an hour (60 minutes, each with 60 seconds = 60*60 = 3600), so just subtract:
$timestamp = time();
$timestamp_less_one_hour = $timestamp - 3600;
You can also use strtotime to accomplish the same:
$timestamp_less_one_hour = strtotime('-1 hour', $timestamp);
Or if $timestamp is simply "now" you can call strtotime without the second parameter:
$timestamp_less_one_hour = strtotime('-1 hour'); // time() - 1 hour
So you seem to be looking for GMT time instead, the trouble is GMT can include Daylight Savings Time (DST), and the "GMT" date functions in PHP actually return UTC time, which has no concept of DST. Luckily you can detect if it's DST using PHP, and basically adjust appropriately.
Get UTC time using gmdate:
$utc_time = gmdate('U'); // return Unix seconds in UTC timezone
Detect if it's DST using I:
$is_dst = gmdate('I'); // 1 when it's DST, 0 otherwise
gmdate('U') will trail GMT time during DST by an hour, thus you need to give it +3600 seconds, or 1 hour when it's DST, so putting this together:
$gmt_time = gmdate('U') + (3600*gmdate('I'));

Categories