Removing an hour from PHP (FROM_UNIXTIME) - php

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'));

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 calculating time difference, result 1 hour wrong

I want to calculate difference of two times and print it in pretty way. I know the difference will never exceed 24 hours, so I tried the following
$time = 7200; //time difference in seconds
date("H:i:s", $time); // should print 02:00
The result is unexpected, it prints 03:00 instead.
What am I doing wrong?
Why not the DateTime::diff??
Here, check PHP.net for DateTime class
Grabbed from php.net datetime diff page:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
//Outputs +2 days
The PHP date() function is meant for formatting absolute time stamps (as in "Friday, 20 July 2012, 15:02 UTC"), not for time differences (as in "3 hours and 5 minutes ago").
In some cases, it is possible to trick date() into producing something that looks like a correctly formatted time difference by setting your time zone to UTC and relying on the fact that the Unix epoch happens to fall on midnight in UTC. However, even then, this will only work for positive time differences of less than 24 hours.
Instead, the correct solution is to use a function designed for formatting time differences, or write one yourself. Here's a simple function to do so:
function format_time_difference ( $delta ) {
$sign = ( $delta < 0 ? "-" : "" );
$delta = abs( $delta );
return sprintf( "%s%02d:%02d:%02d", $sign, $delta / 3600,
($delta / 60) % 60, $delta % 60 );
}
Of course, you can extend this function to e.g. include days in the output if you like.
You should never use date to compute time difference. Firstly it is ugly hack. It was not intended for that purpose. And secondly, it works reliably only when timezone set to UTC.
Now, why it does not work:
PHP function date takes two arguments, format and timestamp, the latter is defined as number of seconds from 1st January 1970 00:00 UTC called unix epoch. So if you call date("H:i", 3600) and your timezone is set to UTC, it will return "01:00", cause it represents time one hour after unix epoch and the epoch was at the midnight.
The problem is, unix epoch was at the midnight only in UTC, not in the other timezones. And this is the source of the incorrect result.

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.

PhP: unix timestamp counting backwords problem

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);

Categories