how this code work ?? and what is the right code - php

This is my current code that doesn't seem to work correctly.
echo date("h:i", 1*60*60) ; /* 1*60*60 mean 1 hours right */
The result is 03:00 when it should be 01:00.
Where is this going wrong?
i want to do this
1- employee come in time admin select it
2- employee come - leave saved in mysql as timestamp
Now I'm trying to make admin see how many hours user late
mean
user date time default late
user1 11-09-2011 09:10 09:00 10 min
user1 12-09-2011 08:00 09:00 -60 min

If you output just date("Y-m-d H:i:s",0) you should see that it's not 1970-01-01 00:00:00 as it should be. It's because date is affected by your local timezone. For example I'm in GMT +3, and date("Y-m-d H:i:s") gives me 1970-01-01 03:00:00.
So the answer is you are not in GMT timezone (probably in GMT+2), and date is affected by it.
UPDATE
The following code outputs 1970-01-01 00:00:00, so it's definitely time zones.
date_default_timezone_set('UTC');
echo date("Y-m-d H:i:s", 0);
Hovewer, I can't see any mention about it in PHP's date manual.

The problem is due to your timezone (looks like GMT+2).
Date calculations in PHP are based on the configured server timezone. For example mine shows
$ php -r 'echo date("h:i", 3600), PHP_EOL;'
11:00

The second argument in date() function must be UNIX timestamp, seconds passed from Jan 1 1970. You need to make time according to that value.

You have probably not setup time zones, which should produce a PHP warning or notice if omitted.

It occurs to me that what SamarLover think he wants is
gmdate("h:i", 1 * 60 * 60);

Err, if I'm right, date()'s second param is a unix timestamp, so the seconds since 1970.
You have to get time() and add 60*60 to it.
echo date("h:i", time()+60*60); // get current timestamp, add one hour

Related

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.

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

Server Date resolving issue

$eve['start']['dateTime'] = 2013-05-02T14:00:00+05:30;
$current_date = date("m-d-Y",strtotime($eve['start']['dateTime']));
$start_time = date("H:i A",strtotime($eve['start']['dateTime']));
when i use the above code am getting it as 05-02-2013 08:30 AM
But i should get 05-02-2013 2:00 PM
why this time difference and shows wrong any idea?
The time difference is not wrong. You are getting the correct date and time for a timezone at +0:00. To fix this, set your timezone.
your formatters seems to be incorrect, to get the desired output use the code below. H is used for 24 hour format with leading zero. h is used for 12 hour format.
date("h:i A");
$eve['start']['dateTime'] = "2013-05-02T14:00:00+05:30"; // Missing quotes in your code?
$current_date = date("m-d-Y",strtotime($eve['start']['dateTime']));
Should return 05-02-2013 08:30 AM because your server has timezone sat to GMT+0. If you take 14:00 and subtracts with 5 and a half hour (from +0530 to +0000), it should be 08:30.
To avoid this, you have to set default timezone on your server or in your script.

date() function outputting wrong result - PHP

Here's what i did -:
Generated the UNIX_TIMESTAMP result in mysql. It came out
1360756718 seconds.
Since I am in GMT +5.30, got the number of seconds in 5 hrs and 30 mins. Result was 19800 seconds
Ran this function -> date('j M Y H:i:s',(1360756718+19800)).
But unfortunately, the answer was 1 hour ahead of original time. And I mean exactly 1 hour. Result was 13 Feb 2013 18:28:38 which should have been 13 Feb 2013 17:28:38.
Where am I worng?
Well, you are in +5:30 but with daylight savings time, we still are 1 hour back. so you now are only +4:30
I think your timezone is set incorrectly. You need to dig around your Apache/PHP/Mysql settings to find out what it is set to.
echo date( 'd.m.Y H:i:s', '1360756718' ); // 13.02.2013 13:58:38
echo date( 'd.m.Y H:i:s', ( '1360756718'+19800 ) ); // 13.02.2013 19:28:38
For me this came out as planned. Are you sure that you started with the right time? As some already mentioned, the problem most likely lies in your timezone or daylight savings time.
Please check your current timezone first.
Here you can check all supported timezones as well
You could try adding:
ini_set('date.timezone', 'My/Timezone');
Since php 5.1 it's also possible to do:
date_default_timezone_set ( string $timezone_identifier )
http://www.php.net/manual/en/function.date-default-timezone-set.php
To set the time to your own timezone. The timezones that are available for php are here:
http://www.php.net/manual/en/timezones.php
the other answers say you should check and correct your timezone settings.
I would do that because it's more correct (regarding to daylight saving time and so on).
If you want to have the easy way try gmdate function:
string gmdate ( string $format [, int $timestamp = time() ] )
This one will always calculate in GMT - you don't have to check the server settings.
(However it will ignore Daylight Saving Time too.)

Why is PHP date() adding +1 hour in diff calculation?

I've got kind of a tricky question, I already searched every related question on Stackoverflow and neither solved my conundrum, although I think I'm running in circles, so here's the question:
I've got this code:
$val = (strtotime('2010-03-22 10:05:00')-strtotime('2010-03-22 09:00:00'))
This returns correctly $val = 3900 (3600 seconds = 1 hour, 300 seconds = 5 mins)
But doing this:
echo date("H:i",$val)."<br>";
returns 02:05
even doing this:
echo date("H:i",3900)."<br>";
returns 02:05 (just to be naively sure)
Doing this:
echo date("H:i eTO",3900)."<br>";
returns
02:05 System/LocaltimeCET+0100
Which is correct, my timezone is CET and is +1.
What's going on? Is date() correcting the timezone for some reason? Or am I doing anything wrong?
This is happening because using date(, ) returns epoch (00:00:00 01 January 1970 UTC) + the number of seconds in the timestamp. It will localise itself to your timezone, so if you provided it with a timestamp of 0 it would return 01:00:00 01 January 1970 UTC+1.
Yes, it is correcting the timezone. When you do
$val = (strtotime('2010-03-22 10:05:00')-strtotime('2010-03-22 09:00:00'))
, what's stored in $val is a timestamp for 01:05, 1 Jan 1970 UTC. See Wikipedia's article on Unix Time.
If you're working with the difference between two timestamps, I'd suggest using DateTime::diff (requires PHP 5.3).
I did this:
date_default_timezone_set('Europe/Helsinki');
Which is GMT+02:00
and the result was:
03:05 Europe/HelsinkiEET+0200
So, it IS actually correcting for timezone, the explanation now I found to be pretty simple (I had an epiphany): date() counts seconds FROM "1 Jan 1970 GMT" so actually 3900 in my timezone and example is correctly "02:05" from that date...
Self learning +1 -_-'
This is actually the right behavior, because date works with local time, and you are on GMT +1. You give it a timestamp (3900) which is 1/1/1970 1:05 and it just ads 1 to get it to your timezone.
If this is your intended use, than you can just subtract the GMT offset of your machine to get the right value.

Categories