I am having all sorts of issues trying to find matching formats to compare against and I can't understand how it can be so hard to compare 2 x dates and I guess it's just above me.
I have a date in a MySQL database: 20/12/17
The report date is: 19/01/18 (it will eventually be todays date but I am testing on an old file)
I just want to find dates in 0-30 days, 31-60 days, 61-90 days and 91+ days
I convert the dates to timestamp so I can do a comparison > or < but I'm having trouble moving the date to 30 days.
I tried converting to a DateTime and using date_sub but it seems to convoluted...
All I want to do is check to see if the date string in the db is within one of the date ranges but i'm struggling.
I try using strtotime with '-30 days' I tried DateTime with date_sub
I think I am just really confused now and can't figure anything out
Here is what I have:
$RO_Date = date_format( date_create_from_format( 'd/m/y', $RO["RO Date"] ), 'Y-m-d' );
$todaysDate = date_format(date_create_from_format('d/m/y', '19/01/18'), 'Y-m-d' );// Using date file was sent
echo $RO_Date;
echo ' ('.strtotime( $RO_Date).') ';
echo ' <br> ';
echo $todaysDate;
echo ' (' . strtotime($todaysDate) . ') ';
echo ' <br> ';
echo ' -30 Days ';
echo ' (' . date("Y-m-d", strtotime("-30 days", $todaysDate)) . ') ';
if (strtotime($RO_Date) >= $todaysDate-30 ) :
echo ':CURRENT<br>';
elseif (strtotime($RO_Date) >= $todaysDate-60 ) :
echo ':31-60<br>';
elseif (strtotime($RO_Date) >= $todaysDate - 90 ) :
echo ':61-90<br>';
elseif (strtotime($RO_Date) >= $todaysDate - 120 ) :
echo ':90-120<br>';
else :
echo ':120+<br>';
endif;
Output:
2017-12-20 (1513688400)
2018-01-19 (1516280400)
-30 Days (1969-12-02) :CURRENT
2018-01-03 (1514898000)
2018-01-19 (1516280400)
-30 Days (1969-12-02) :CURRENT
2018-01-05 (1515070800)
2018-01-19 (1516280400)
-30 Days (1969-12-02) :CURRENT
2018-01-12 (1515675600)
2018-01-19 (1516280400)
-30 Days (1969-12-02) :CURRENT
2018-01-18 (1516194000)
2018-01-19 (1516280400)
-30 Days (1969-12-02) :CURRENT
2018-01-18 (1516194000)
2018-01-19 (1516280400)
-30 Days (1969-12-02) :CURRENT
<?php
$todaysDate = date_format(date_create_from_format('d/m/y', '19/01/18'), 'Y-m-d' );// Using date
$todaysDate = strtotime($todaysDate);
echo $todaysDate . PHP_EOL;
$todaysDate = date("Y-m-d", strtotime("-30 days", $todaysDate));
echo $todaysDate;
Output:
1516316400
2017-12-20
In this line
$todaysDate = date("Y-m-d", strtotime("-30 days", $todaysDate));
$todaysDate needs to be a timestamp not a date string, that was the mistake
<?php
function dateDiff($dateStringFromDB){
$today = new DateTime("Now");
$db_string = new DateTime($dateStringFromDB);
$days = $today->diff($db_string)->format('%d');
return $days;
}
?>
The function above calculates the difference between the current date and any date passed to the function and returns the number of days. Example: if $dateStringFromDB is 17/04/2018 and $today is 15/04/2018, it returns the value of $days as 2.
use:
echo dateDiff($dateStringFromDB);
You can now use the result for whatever you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.