I have been doing a lot of work on dates in php and everytime I struggle with the date conversion for over an hour before finally getting something to work but it is more a fluke than anything else.
I am now trying to actually understand what I am doing (and understand what I am not doing) that is making this process so difficult for me.
I am just trying to implement an expiry.
- I have a date in a file in the format: 30/06/18 (30th June 2018)
- I have an expiry days (int): 30
- I have today’s date: date(‘d-m’Y’);
What I want to do is:
- Convert the string to a date
- Add “30” days to the date
- Test against today’s date to see if it has expired
Here is what I have done (which appears to work)
$expiryDays = $this->wtyCalcConfigData[$RO["Category"]]["exp_days"]; // Number of days to expire Warranty Claim e.g. "20"
$expiryDaysFormat = "+" . $this->wtyCalcConfigData[$RO["Category"]]["exp_days"] . " days"; // Create string for date calc: "+25 days"
$calcFieldDate = date_create_from_format('d/m/y', $RO['Date'])->format('d-m-Y'); // Convert String into PHP Date e.g. "11-07-2018"
$expiryDate = date("d-m-Y", strtotime($expiryDaysFormat, strtotime($calcFieldDate)));
$today = date("d-m-Y");
$dateDiff = round( (strtotime($expiryDate)-strtotime($today)) / (60 * 60 * 24) );
if ($dateDiff > 0) echo "Ok";
else echo "Expired";
Is there a simpler, more flexible way of managing dates? Have I reached the best solution or does this look like i’ve gone about it the hard way (It definitely feels like it was the hard way)