Negative strtotime [duplicate] - php

This question already has answers here:
why strotime returns negative value in php?
(6 answers)
Closed 9 years ago.
Having Europe/Amsterdam as timezone.
Why does:
strtotime("January 1, 1970 00:40:00');
returns
-1200
I don't get it.

Because, php counts time starting from January 1, 1970 00:00:00 . So you are back from the reference time, That's the possible reason. So 1200 would be
( - 60*40 (+/-) time_difference_in_your_timezone)

The strtotime function parses the strinf date into "into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)". Please see the php documentation.

The -1200 is caused by your time zone.
strtotime is calculating from January 1, 1970 00:00:00 UTC. If you calculate the date back from UTC+1 to UTC you are at December 31, 1969 23:40:00 UTC.

Related

converting iso time string returns wrong date time object in php [duplicate]

This question already has answers here:
PHP strtotime for June returns July
(3 answers)
Closed 2 years ago.
Converting iso string in zulu time returns the next time. I'm not sure what I've done wrong please. see example below
$dateTime = "2020-04-31T23:59:58Z"
(new DateTime($dateTime)); // returns 2020-05-01 23:59:58.0 +00:00
April has 30 days.
As such, DateTime returns "April 31st" as May 1st (April 30th + 1 day).

Convert date format 1542575966120 18/11/2018 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a DB that shows when the user made the last login but then it shows 1542575966120. I wanted it to show so 18/11/2018 19:00
I tried using this in php
$intDate = "20". $ infologado ["lastlogin"];
$newDate = date ("d-m-Y", strtotime ($ intDate));
but I could not.
sorry for English
So as #Taha Paksu had mentioned, these numbers are a timestamp (seconds since 1 January 1970). Try this code:
$intDate = 1542575966120;
$newDate = date('d/m/Y H:i', $intDate/1000);
It is in miliseconds, date function accepts seconds, thus the division by 1000. Also no need to put it into strtotime, because this function is meant to convert string dates to... said numeric timestamps.
In your case, you can put $intDate = $infologado['lastlogin']; instead of first line to get the result dynamically from the database.
First of all, you need to learn what a timestamp is. The timestamp is a number which shows the seconds passed (or milliseconds, some include the milliseconds too) since epoch (01/01/1970). A general definition can be found here:
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for 'Unix time'. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).
The converter on this page converts timestamps in seconds, milliseconds and microseconds to readable dates.
Taken from: https://www.epochconverter.com/ a tool which you can convert your dates to/from timestamps or vice versa.
Then to answer your question, the system saved the dates as a timestamp to the database to (probably) bypass the formatting errors on each different system that uses it.
Nevermind, TL;DR:
The number shows Sunday, 18 November 2018 21:19:26.120 when you give it to the timestamp converter I mentioned above. With PHP, you can use:
$unixTimestamp = 1542575966120;
$dt = DateTime::createFromFormat("U.u", $unixTimestamp / 1000);
var_dump($dt);
to convert to PHP DateTime class, then you can use it in your application.

Calculating age in php [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
PHP: strtotime is returning false for a future date?
(5 answers)
Closed 8 years ago.
I need to calculate age in php.
I use this solution:
$birthdate = '1986-09-16';
$_age = floor( (strtotime(date('Y-m-d')) - strtotime($birthDate)) / 31556926);
from here
Everything works fine, but for example if
$birthday = '1194-01-06' or
$birthday = '1900-01-01'
result is always 44.
if $birthday = '1910-11-09' everything is fine again and result is 103. Why?
Note: I don't want to use diff() function, because of some issues.
EDIT:
Earlier i had problems with diff(), some
Warning range()
showed during processing and after refreshing of website everything was fine again... i could not find solution to fix it and somewhere i read that using of diff() could cause it. So i tried other solution and it worked... until now.
Finally I used this solution:
$birthDate = from database in timestamp format...
$birth = new \DateTime($birthDate);
$now = new \DateTime;
$age = $now->diff($birth)->y;
and I randomly get
Warning
range(): step exceeds the specified range
again.
It's because you're using date that is using timestamp that has a default value of time() that is based on EPOCH that started on January 1 1970 00:00:00 GMT - it's 44 years since 1970.
More on this can be found in the PHP Manual: http://php.net/manual/en/function.date.php
Integer limit issue, either:
Your OS doesn't handle negative timestamps
The maximum integer values for signed integers on a 32 bit system
strtotime()
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)
Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch (1 Jan 1970).
Have you read strtotime() manual (https://php.net/manual/en/function.strtotime.php)?
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.

Which type of date format it is?

Currently I am implementing one of API.Which returns date as 1389914586 .I don't know which type of date format it is? Whether it is offset format or any other
It's the number of seconds since midnight Jan 1, 1970, UTC.
You can see what it is displaying by using PHP's date format.
<?php
print date("Y-m-d H:i:s", 1389914586);
That will output:
2014-01-16 16:23:06
This is called unix time, calculated as the number of seconds from Thursday, 1 January 1970.

Epoch to date is 3 years off

i get a epoch time returned from a webservice wich is about 3 years off in PHP but fine in javascript and the epochconverter.com
JS:
alert('book '+ new Date(1285565357893)); // returns a time this morning 27 sep 2010, Correct!
PHP:
echo strftime('%x', 1285565357893); // returns a date in 2013, Wrong !
Timezone is set to: Europe/Amsterdam
What am i doing wrong here ?
OK, some simple time basics for you.
Javascript Date class... when you pass a numeric value to the constructor, this is the number of milliseconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
PHP date is measured as the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Convert from milliseconds to seconds in PHP by dividing by 1000.
echo strftime('%x', floor(1285565357893/1000));

Categories