Subtracting dates in PHP [duplicate] - php

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;

Related

How to convert a date in the format DD-MM-YYYY_HH-MM-SS in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
Here are 3 examples of date and time strings I have
08-01-2019_03-00-34pm
22-01-2019_02-05-06pm
30-01-2019_11-17-07am
I would like to convert these to Unix Time Stamps
For example I have tried this on the first one..
echo (strtotime("08-01-2019"));
This half works, as it gives me a timestamp of 1546905600 which translates back to the correct date, but obviously makes the time 12:00:00am by default. So I'm stuck with how to deal with the time bit
I understand there is a str_replace() function so as long as I know what format I need to make the time bit, I guess I could use that?
Use DateTime::createFromFormat():
$date = DateTime::createFromFormat('d-m-Y_h-i-sa', '08-01-2019_03-00-34pm');
You can then use it to get the timestamp
$date->getTimestamp(); // 1546977634

How to calculate time difference between two dates [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
How to Calculate time difference between two dates in php?
$date1=date('Y-m-d H:i:s');
$date2=date('2015-03-06 45:06:03');(Db)
am trying to do once time difference between 2 hours send Mail to user.Current datetime and Db datetime difference between 2 hours mean Send Mail to user.How to Check?
Did you try $diff=$date1 - $dates?
strtotime — Parse about any English textual datetime description into a Unix timestamp and divide by 3600 to find result in hours. Try like this..
$date1=date('Y-m-d H:i:s');
$date2=date('2015-03-06 45:06:03');
echo $diff_hours = (strtotime($date1) - strtotime($date2))/3600;

Subtract seconds from a specific given time PHP [duplicate]

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

PHP check if 1 day passed [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture
What I want to do is to get day from database, current date. And check if difference between them > 1 day:
$curdate= date("Y-m-d H:i:s");
$dbdate is value stored in datetime format in db.
$dif=$curdate-dbdate;
How to check if $dif>1 day ??
Assuming the stored date is expressed in the same time zone as the server, you can convert it to a timestamp using strtotime, and compare it to strtotime("-1 day"):
if (strtotime($dbdate) < strtotime("-1 day"))
frobnicate();
You can get just the day from each date.
$day = intval($curdate= date("d"));
This will get the day as an in. Do the same for the time of the data base and you get two integer representing the day. Using that you can calculate how many days have pass.
Beware that the last line should look like this:
$dif = abs($curdate-$dbdate);

Time since in php dateTime object in seconds [duplicate]

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?

Categories