This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP How to find the time elapsed since a date time?
I have the following which produces MONTH YEAR.
<?php echo date('F Y', strtotime($video['Video']['posted_date'])); ?>
I'd like this to output to "Time" ago. i.e 3 hours ago, 6 months ago, 2 years ago.
Any help will be very much appreciated.
Thanks
Andy
$now = new DateTime(date("Y-m-d H:i:s"));
$videoPosted = new DateTime($video['Video']['posted_date']);
$interval = $now->diff($videoPosted);
echo $interval->format('%h hours, %m month and %d days ago');
Use only if PHP version > 5.3
If you use php 5.3+, you are looking for DateInterval::format()
this will help you to calculate date difference: How to calculate the difference between two dates using PHP?
and after that you can convert the result using strtotime()
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:
How to save dates older than timestamp in PHP
(3 answers)
Closed 3 years ago.
I am looking to have a one line return function to get the date 150 years ago from today.
I currently have,
return date('Y-m-d', strtotime('-150 year'));
The problem is this returns 1970-01-01 instead of 150 years ago.
Why is this?
Your issue is that strtotime returns a unix timestamp (seconds since 1970-01-01 00:00:00), so the earliest date it can understand is 1970-01-01. Since 150 years ago today is before 1970-01-01 strtotime is returning false, which is interpreted as a 0 timestamp by date, resulting in an output of 1970-01-01. To work around this limitation use the DateTime class:
$date = new DateTime('-150 year');
echo $date->format('Y-m-d');
Output (as of 2020-02-19):
1870-02-19
If the code does need to be in one line, you can write:
$date = (new DateTime('-150 year'))->format('Y-m-d');
Demo on 3v4l.org
This question already has answers here:
Calculate difference between two dates (number of days)?
(17 answers)
Finding the number of days between two dates
(34 answers)
Closed 8 years ago.
I want to calculate days between 2 month.1 date is set that is 01-03-2014 and second date will be given by user.This may be any date for example 14-05-2014.Now i want to calculate days between these 2 dates?
Thanks
You can use DateTime for this, and do a simply calculation. See DateTime:diff
<?php
$start= new DateTime('14-05-2014');
$end= new DateTime('01-03-2014');
$interval = $start->diff($end);
echo $interval->format("%a total days"); //Output: 74 total days
https://eval.in/236630
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP iterate days of month given month and year
"Obtain all days in month with the number of months"
For example i have date 2011/12/16 this date have months 12(december) now i want with code php know that, This month(12 or december) is a few days?(29? or 31? or 30?)
How is it by PHP?
$mydate = strtotime('2011/12/16');
$daysInMonth = date('t',$mydate);
Edited to add strtotime()
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
How to count days between two dates in PHP?
Full Days between two dates in PHP?
how i can know how many days between 2 date
ex:
i want know how many days between 12\02\2011 and 15\03\2011
please help me
<?php
$datetime1 = date_create('2011-02-12');
$datetime2 = date_create('2012-03-15');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
You dates just have to be of one of the following formats:
PHP Date formats
http://ca.php.net/manual/en/datetime.diff.php
look at the example. it gives you pretty much exactly what you asked.