Manipulating dates


#1

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.

  1. I have a date in a file in the format: 30/06/18 (30th June 2018)
  2. I have an expiry days (int): 30
  3. I have today’s date: date(‘d-m’Y’);

What I want to do is:

  1. Convert the string to a date
  2. Add “30” days to the date
  3. 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)


#2

Hi there!

For a first try, this is pretty decent, but as you said, this is the long way to do it.
I do not know how familiar you are with Object Orientated Programming, but we have a class: DateTime ( see: Click me! ) that will handle a lot of the extra work you’re doing here.

I think, in your use-case this would be a better solution to your code:

$now = new DateTime();
$expiryDate = DateTime::createFromFormat('d/m/y', $RO['Date']);
$expiryDateInterval = new DateInterval('P' . $this->wtyCalcConfigData[$RO["Category"]]["exp_days"] . 'D');
$expiryDate->add($expiryDateInterval);
        
if ($now > $fieldDateObject) {
// do something
 }

Forgive me if I made a typo somewhere, this is just written from the top of my head.

So, as you can see, we are doing the following:

  1. We are creating a new DateTime object for today’s datetime
  2. Then we are creating a new DateTime object from format (string) for your $RO[‘Date’], with format: d/m/y
  3. Then we are creating a new DateInterval object, and setting it to: $this->wtyCalcConfigData[$RO[“Category”]][“exp_days”] days (see: D at the end)
  4. Now we are adding the DateInterval object to the DateTime object, and letting the DateTime class do all the work for us.
  5. We can simply use the 2 DateTime objects in an if statement with a “greater than” operator, and let PHP solve the calculations for us.

I hope this will give you enough insights in your question, and if you have additional questions, please feel free to ask them!