I’m trying to get a simple “XX days left” for a custom post time I’m working with (jobs). I’ve tried to find the answer everywhere but nothing adapts well, changed my code 1000 times following examples on internet but I couldn’t solve.
My code looks like this :
<?php
$today = date('d/m/Y');
$today = strtotime($today);
$finish = get_field('todate');
$finish = strtotime($finish);
//diference
$diff = $finish - $today;
$daysleft=floor($diff/(60*60*24));
echo "$daysleft days left";
?>
on $finish the get_field (‘todate’) returns the date dd/mm/yyyy
Anyone who can help me please?
Thanks!
The issue is the arrangement of the d/m/Y (it should be m/d/Y) in your $today and $finish variables (for a listing of valid date formats that work with strtotime, visit the PHP Date Format page):
Tested and works, assuming the $finish date is entered as m/d/yyyy (not as d/m/yyyy)
strtotime
will only parse a date in day-month-year format if the values are separated by dots, tabs, or dashes. See Date Formats.If you have PHP 5.2 or greater, you can use the following function to get total days:
This takes into account DST and leap years.
How about this function?