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;
Related
This question already has answers here:
Finding the number of days between two dates
(34 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 months ago.
i am beginner at php. I want to calculate exact total number of days between two dates using php, mysql and want to show it in html form. I tried datediff but it doesn't works as it gives , diffrence not total number of days.
Date fm - 10-10-22;
Date to - 20-10-22;
Total Days - 11
add + 1 to date_diff the output will 11 be like :
<?php
$d1=date_create("2022-10-10");
$d2=date_create("2022-10-20");
$diff=date_diff($d1,$d2);
echo $result = 1 + $diff->format("%R%a days");
?>
php has inbuilt DateTime functions to get the difference. Below outputs 10 day difference.
$date = new DateTime('2022-10-20');
$next_date = new DateTime('2022-10-30');
echo $date->diff($next_date)->days;
This question already has answers here:
Find difference between two datetimes and format at Y-m-d H:i:s
(7 answers)
Closed 5 years ago.
I have a date in this format: '2017-09-26 00:02:37'. I am trying to calculate the difference of this date and now, and get the value in months.
Eg. (52 weeks)
What is the proper way of achieving this?
There is a function in Carbon called diffInWeeks()
$date->diffInWeeks($otherDate);
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 6 years ago.
I want to count how many hours, minutes and seconds between two dates.
For example: Date 1: 01-01-1990 12:00 and 01-02-1990 13:00
Now, how many hours, minutes, and second from date 1 to date 2 using php?
just use strtotime() on your dates to their timestampz . and then subtract them to have the difference in the number of seconds. it's easy to convert from there
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:
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;