PHP Date - Weird date calculation - php

I was trying to round down time to a whole minute (as part of bigger rounding mechanism). In my unit tests I figured something strange, this peace of code:
echo date('Y-m-d H:i:00', strtotime('2018-09-31 19:39:45'));
result in:
2018-10-01 19:39:00
What am I doing wrong?
Note: I WAS running this on 2018-10-01
http://sandbox.onlinephpfunctions.com/code/1cb0dd98e9d540616d02ce2d5c00684800af8597

strtotime() doesn't validate dates.
If you do
echo date('Y-m-d H:i:00', strtotime('2018-02-30 19:39:45'));
Outputs
2018-03-02 19:39:00
So, the problem here is that you are using an "invalid" date, and PHP is summing up seconds.
So, the spetember 31 means september 30 + 24 hours (in seconds). When you run date() will get the date in seconds and showing to you the valid date, october first.

Related

Counting down days not showing the right number of days

I need help.. Is this right?
Start Date: Mar 16, 2014
End Date: Mar 19, 2014
Results: 2 Days
$plantEnd = get_the_author_meta('plantEnd', $sellerID );
$plantStart = get_the_author_meta('plantStart', $sellerID );
$future = $plantEnd;
$d = new DateTime($future);
echo $d->diff(new DateTime())->format('%a').' Days';
Why does it says 2 days? Isn't it 3 days? Im confused..
Since you aren't actually using $plantStart in your code and instead using the current time, you're basically getting a difference between now (the time the script was run, on server's time zone) and the start of Mar 19, 2014 (0h:0m:0s). So what you are really getting is something like 2 days 5 hours 3 minutes 25 seconds (depending on when you run it vs. server time.
for example, when I run this locally:
$d->diff(new DateTime())->format('%d:%H:%i:%s');
I get 2:04:59:25
So there's more to it than just getting that "2" returned.. you're just not formatting for it.
And again, you aren't actually using the $plantStart anywhere either. So if you were to do this:
<?php
$plantEnd = '2014-03-19';//get_the_author_meta('plantEnd', $sellerID );
$plantStart = '2014-03-16'; //get_the_author_meta('plantStart', $sellerID );
$future = $plantEnd;
$d = new DateTime($future);
echo $d->diff(new DateTime($plantStart))->format('%d:%H:%i:%s');
?>
You will see it outputs 3:00:0:0 (or you could continue to just use %d and get the "3"). This is because $plantStart (presumably - based on your post) just specifies yyyy-mm-dd, so passing just the yyyy-mm-dd value will put the hh:mm:ss at 0:0:0 (beginning of day) , so it will be a full day's calculation, which has the effect of "rounding up" to the whole day increment.
I have a feeling that it's actually 2 days, someodd hours, and someodd minutes, or something to that effect. Because you're formatting to just do days, you're losing the nuances. I'd change the code to say "2.4 days" (and for the life of me I can't remember how I did this in the past...)
EDIT: in the past I have simply used date() instead of DateTime().
I did a little research, and you might want format('%d')." Days";

PHP String to Time off by one hour

I am working with the PHP date and time functions to create my own time between dates calculator and have encountered a problem with the strtotime() function.
When working with a manually entered date and time, calculating the difference between:
20-02-1986 12:00:00 and 04-01-2014 19:31:13
returns what appears to be the correct difference, the first time converted using strtotime() and the second time retrieved using time().
However, if the one of the times is in EDT and the other is in EST, the strtotime() function seems to add an hour as if one of the times is 'falling back' to EST.
17-09-1986 12:00:00 and 04-01-2014 19:37:03
According to timeanddate.com, the difference between the two dates should be:
861,521,823 seconds
but the following code I have produces this:
861,525,423 seconds
There are an extra 3,600 seconds (1 hour). When using strtotime() on 17-09-1986 12:00:00, PHP seems to be working with 17-09-1986 11:00:00.
<?php
date_default_timezone_set('America/New_York');
$today = time();
$pastDate = '17-09-1986 12:00:00';
$pastDate = strtotime($pastDate);
$timeAlive = $today - $pastDate;
?>
You should convert both dates to GMT, so you can do the difference correctly. PHP is detecting that both times are on the same time zone, so it just does the math, disregarding the time zone change. Converting first should get rid of that problem.

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

Wrong output from date()

I am getting the date from my MySQL database and that date is correct, but when I use the date() function the minute part is stuck at 6 minutes.
MySQL is returning 2010-06-15 09:59:18
Then in PHP I use this code ($row[4] is from my while loop):
date('M d,Y H:m A',strtotime($row[4]))
When I echo this out I get: Jun 15,2010 9:06 AM
I also have tried converting it to a unix timestamp in my SQL query, but it does the same thing. I noticed that the hours work and the seconds work but the minutes are stuck at 6. Does anyone have any ideas on what is going on?
'm' is a representation of month, not minutes using the PHP date() function. So, you are getting the '06', meaning June, not 06 minutes past 9.
It only looks like minutes because of how you formatted your date string but what you are getting there is a numerical representation of the month.
http://us2.php.net/manual/en/function.date.php
m - Numeric representation of a month, with leading zeros
I think the minute arg should be i (lowercase eye), not m.
Use this instead (i instead of m):
date('M d,Y H:i A',strtotime($row[4]))
The capital m is the month, and now it's June ;)
The minute is "i"

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