PHP String to Time off by one hour - php

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.

Related

PHP Date - Weird date calculation

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.

PHP: convert UTC Date into seconds - not works with strtotime()

I have this UTC Date 2017-07-16 12:00:07.8 UTC (that calls $dateconvert) and I would like to convert it into seconds.
I tried to use strtotime() but it returns seconds in UTC removing another two hours and I don't understand why.
I'm in Italy and here Date is UTC+2, maybe strtotime() read $dateconvert with Italy time zone and when convert it into seconds removing that 2 hours?
Is it possible?
$anno_emsc= 2017;
$mese_emsc= 7;
$giorno_emsc= 16;
$ora_emsc= 12;
$minuto_emsc= 0;
$secondo_emsc= 7;
$dataconvert= strtotime($anno_emsc.'-'.$mese_emsc.'-'.$giorno_emsc.' '.$ora_emsc.':'.$minuto_emsc.':'.$secondo_emsc)+0;
OUTPUT
1500199207 // strtotime removed 2 hours
Since you already know all the parts, you can use gmmktime() to convert it into a timestamp. The "gm" version of mktime() knows that it's receiving a GMT/UTC date and will process it accordingly.
Try and set your date_default_timezone_set() to your correct timezone and see if that fixes your issue. Sometimes your php instance will default to the wrong timezone.

Is PHP strtotime function when given format of only m/d/Y guaranteed to return first second of that day?

strtotime() returns number of seconds since so and so date. OK. So it's all in seconds. Now, if you give a date format which consists of only day, month and year, what time does it return in terms of seconds. The very first second of the day, the last second or undefined in between? The manual does not provide any guidance and common sense would assume the first second. Why is this significant? It could be when comparing or computing time interval between a fully defined date and a partially defined datetime (one without hours, minutes and seconds).
strtotime("1/1/2014")
Is this "guaranteed," as opposed to expected, to return the very first second of the new year?
It will return the time from 00:00:00, e.g. strtotime("1/1/2014"); = strtotime("1/1/2014 00:00:00");
In case you need to be sure, just use:
strtotime("1/1/2014 00:00:00");
Yes, it will always return first second of that day:
echo date('Y-m-d H:i:s', strtotime("1/1/2014"));
# 2014-01-01 00:00:00
demo
but to be sure, just enforce time like #Pekka suggested:
echo strtotime("1/1/2014 00:00:00");

Date / Time showing odd value in PHP

I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime

PHP error when using strtotime

Hi I get an error when trying to get date interval using php strtotime function
the code is:
<?php
$interval = time() - strtotime('1992/08/13');
//expect to be 18
// but the output is 1988
print date('Y', $interval);
?>
any advice?
thanks
If you want to deal with date intervals in PHP I can't recommend the DateInterval class enough. I wrote a blog post on this earlier this week: Working with Date and Time in PHP
There's an example of using it there that should allow you to do what you want to do.
That is because all time() functions are seconds since epoch which is in 1970, so your out is actually 18 years since epoch. If you want it to get the difference in years you will probably have to calculate the difference yourself.
print $interval / (60*60*24*365.242199);
Are you tring to get the years elapsed rather than the actual year?
If so:
$year = 31556926;
$interval = time() - strtotime('1992/08/13');
print round($interval / $year);
$interval = time() - strtotime('1992/08/13');
These PHP functions deal with UNIX timestamps. That means the number of seconds from 1970. 01. 01. So 1992/08/13 is transformed into a timestamp (seconds). time() gives the current timestamp (seconds). You subtract the former from the latter, and you get the amount of seconds between those two dates. This is not a date itself, just an interval.
If you want to get the year, do something like echo $interval/(60*60*24*365); which will convert your seconds to years (not accurate, leap years will not be taken into consideration). Though your best option is checking out #James C's link and use his solutions. I just wanted to give some explanation.

Categories