开发者

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

开发者 https://www.devze.com 2023-04-08 13:51 出处:网络
This is my current code that doesn\'t seem to work correctly. echo date(\"h:i\", 1*60*60) ; /*1*60*60 mean 1 hoursright */

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 g开发者_如何学运维oing 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号