This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Date to timestamp (PHP)?
How do I get the current time in seconds since 1st january 1970 in php?
does the dateTime object support anything since as
$date1 = new DateTime("01/01/1970");
$return = since($date1, date.now());
The problem I get here is I could just do something like ((minutes=hours *60)*60)
Is there a nicer way?
You can use the time() function:
http://php.net/manual/en/function.time.php
time()
Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
$_SERVER['REQUEST_TIME']; is a pretty light way to do it, if your server supports it. Otherwise you have the usual time() / strtotime() functions. Also, why is there javascript in your PHP?
Related
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.
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.
This question already has answers here:
Subtract one second from a given time
(2 answers)
Closed 8 years ago.
I writing a code for subtract seconds from a time using php. i have date which assigned to variable , i need to subtract seconds from that date.
$date="2014-03-16 17:40:27";
echo date("Y-m-d H:i:s", strtotime($date) - strtotime("-600 seconds"));
but this gives me dates on 1970S, i search everhere and didn't found a answer which matched for my question. can anyone help me to fix this little code
strtotime() gives you a timestamp in seconds. Don't make another timestamp to subtract from it, just take 600 from it:
echo date("Y-m-d H:i:s", strtotime($date) - 600);
//2014-03-16 17:30:27
This question already has answers here:
Dealing with timezones in PHP
(4 answers)
Closed 9 years ago.
I have some php scripts on a hosting, but hosting time is different from my local time (GMT+8)
How set the right time() script to be GMT+8 ?
When i use:
<?
echo time(); //it show me the hosting time;
?>
time() will always return the number of seconds since the epoch. The code below will print the same twice.
date_default_timezone_set('Europe/London');
echo time();
date_default_timezone_set('America/Cuiaba');
echo time();
The concept of Unix Timestamp does not carry time zone information by design. A given timestamp is always the same regardless of time zone. (The number of seconds since 1970-01-01 00:00:00 UTC) When you want to express a timestamp with time zone taken into account, you will adjust the resulting date with the current time zone's offset.
So when using the 'c' format option to PHP's date (which does reflect time zone information) you will see different representation of the same timestamp
date_default_timezone_set('Europe/London');
echo time();
echo date('c')
date_default_timezone_set('America/Cuiaba');
echo time();
echo date('c');
Will output:
1384259474
2013-11-12T12:31:14+00:00
1384259474
2013-11-12T09:31:14-03:00
Your assumption is not correct:
int time ( void )
Returns the current time measured in the number of seconds since the
Unix Epoch (January 1 1970 00:00:00 GMT).
The Unix Epoch is a fixed moment in time. If you really get an invalid timestamp, your hosting provider has not cared to set the server's clock.
If you want to do decent time zone aware date handling I suggest you learn about the DateTime class and friends and:
Use named time zones (Europe/Madrid) rather than UTC offsets (+01:00) since they take DST into account.
Set your app's time zone as default so you don't need to specify it every time:
date_default_timezone_set("Europe/Helsinki");
Never ever do date math yourself (e.g., don't add 86400 seconds manually to increase a day).
You must set the local time in php
function date_default_timezone_set
List of Supported Timezones
Example:
date_default_timezone_set("America/Fortaleza");
echo time(); // Local Time in America/Fortaleza
Add 28800 (8 hrs converted to seconds) to the output of time()
<?php echo (time()+28800); ?>
I am not sure but, time() returns timestamp which is equivalent to GMT thus, adding timestamp of 8 hours will give you GMT+8. Similarly, you can even subtract the time also
You can use date_default_timezone_set() function, For example
date_default_timezone_set('Asia/Dhaka');
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate the difference between two dates using PHP?
I have timestamps stored in the format YYYY-MM-DD HH:MM:SS (for example 2010-06-21 20:12:56). What would the best way to check how old the timestamp is? For the moment I am mainly interested in the number of days old.
You can use strtotime to convert the string to a UNIX timestamp, which is in seconds. time() will give you the current UNIX timestamp. Subtract them to get how old the date is in seconds, and divide by 60*60*24 to get it in days
It's also doable using DateTime::diff, although I find the date functions easier than using the classes
$today = strtotime(date('Y-m-d H:i:s'));
$expireDay = strtotime($row['ExpireDate']);
$timeToEnd = $expireDay - $today;