date() function outputting wrong result - PHP - 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.)

Related

time() function in php says current time is 1970

I am running XAMPP on mac.
I have a very simple script:
<?php
echo time();
always returns time in the 1970.
example: 1475044574 (Edit: 1475137157)
I tried setting the timezone in php.ini and then calling date_default_timezone_get() and it returns the correct set value.
I tried adding SetEnv TZ MyTimezone to httpd.conf (at the bottom of the file) and it did not work.
I also tried setting the timezone in php with date_default_timezone_set() and though it sets successfully, time is still in 1970.
I tried the same script on MAMP and still the same problem.
Any suggestion is appreciated!
time function is returning proper unix timestamp you are doing mistake in converting time into proper format. 1475044574 means Wed, 28 Sep 2016 06:36:14 GMT which is correct.
You can convert time as you want. refer date formats.
check this sample code :
echo date('M j Y g:i A', 1475044574);
Bro, as per PHP manual php.net/manual/en/function.time.php time() function returns seconds not miliseconds. Please check timestamp your timestamp on this site epochconverter.com.
time() function returns current Unix timestamp. So, your 1475044574 is a timestamp.
Using date function you can see that this is a timestamp of (some variations with timezones, results may vary):
echo date('Y-m-d H:i:s', 1475044574);
// 2016-09-28 02:36:14
If you convert this timestamp to some string representation and get something like 00-00-1970 - you obviously do the conversion wrong.

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

PHP Date / GMT Weirdness

Either I'm losing my mind, or I've not got the faintest idea what I'm doing. I'm leaning towards the latter.
I'm trying to convert this: 1316826000, which I'm pretty confident should be Sat, 24 Sep 2011 01:00:00 GMT
http://www.onlineconversion.com/unix_time.htm confirms this.
http://www.unixtimestamp.com/index.php tells me 09 / 23 / 11 # 8:00:00pm EST, so far so good. I happen to be in EST, this is the result I'd like to get back from PHP.
When I do date('l, M d, Y, h:ia', $iTime), I get: Friday, Sep 23, 2011, 12:00am, a full 20 hours off.
I've confirmed the server's time is correct using date('c'). date('c') output is: 2012-05-19T03:19:20+00:00. The server is in the central time zone, where it is currently 10:20pm. May 18.
echo date_default_timezone_get() outputs "GMT" (set somewhere else in the script using date_default_timezone_set('GMT'))
What am I missing? Nothing I've read so far can explain how I'm getting a result 20 hours behind what it should be. Were it an hour fast or slow, I could at least wrap my head around it being some sort of DST idiotry, but 20? Crazyness! Thanks for reading!
Check what your php.ini says for date.timezone.
In unix it is usually here: /etc/php.ini
Then use a proper timezone recognized by PHP:
http://www.php.net/manual/en/timezones.php
date.timezone = 'America/New_York'
Then reload your web server.
Unix time just means the number of seconds since epoch. Has nothing to do with timezones. Timezones simply add or subtract 1 hour (3600 seconds) from the unix time for each zone you move away from GMT.
An example:
$userTimezone = new DateTimeZone('America/New_York');
$gmtTimezone = new DateTimeZone('GMT');
$myDateTime = new DateTime('2014-01-22 11:44', $gmtTimezone);
$offset = $userTimezone->getOffset($myDateTime);
echo $offset;
That will output: -14400 or 4 hours. Which is the difference between New York and GMT
Using some Java code with the Joda-Time 2.3 library, as I don't know PHP…
long m = 1316826000L;
DateTime dateTimeUtc = new DateTime( m * 1000L, DateTimeZone.UTC );
DateTime dateTimeNewYork = dateTimeUtc.toDateTime( DateTimeZone.forID( "America/New_York" ) );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeNewYork: " + dateTimeNewYork );
I can tell you that 1316826000 seconds from the beginning of 1970 UTC/GMT (Unix Epoch) is…
dateTimeUtc: 2011-09-24T01:00:00.000Z
dateTimeNewYork: 2011-09-23T21:00:00.000-04:00
So, as the commenter stated, it would be 8 PM in EST but EST was not in effect on that day. DST (Daylight Saving Time) (idiocy, as you correctly noted) was in effect until November 9 of that year (2011). So the time of day is pushed forward one hour, to 9 PM.
In GMT/UTC, that means 1 AM in the morning of the next day.
Standard time in east coast US is 5 hours behind UTC/GMT. With DST it is 4 hours behind UTC/GMT (one hour closer).
Where you got confused:
Your time format/conversion in incorrect.I can't help with that as I don't know PHP.
You should be using a competent date-time library for this kind of work.Date-time work is complicated, tricky, confusing, and error-prone.This question discusses possibilities of Joda-Time (for Java) sorts of libraries for PHP.
You used three-letter time zone codes. Avoid these.Those codes are neither standardized nor unique -- there are common duplicates. Instead, use proper time zone names. In your case of east coast US, "America/New_York". Furthermore, in this case you confused the time zone area and rules (east coast US) with a particular application of those rules (EST). Saying "America/New_York" means "whatever time zone rules were in effect on that date, whereas saying "EST" (if interpreted to mean Eastern Standard Time in US) means "UTC-05:00". So either (a) use a time zone name such as "America/New_York", or (b) use a specific offset such as "-05:00".

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

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

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