Skip to content

PHP – Increment a number daily from a date

Error –

How to increment a number daily from a date since strtotime function is now deprecated since php 8.1

Following program will not work now.

<?php
     $now = time();
     $your_date = strtotime("2010-01-01"); //Starting date
     $datediff = floor(($now - $your_date)/(60*60*24));
echo $datediff;
?>

Environment –

  • PHP 8.1 and above
  • WordPress 6 and above

Root cause –

As already shared above strtotime function is deprecated in php 8.1 and above. So, it is recommended to use DateTime() to get start date for counting number of days from a date.

Solution –

Use following code to increase a number daily from a date.

<?php
$startday = new DateTime('2020-08-30');
$today = new DateTime();
$days  = $today->diff($startday)->format('%a');

echo $value = $days;
?>

Do you have another solution?

The solution provided above is based on the scenario our one of the developers/contributors faced. If you faced the same issue and found any other root cause then please share your solution in the comment section below. We will add the solution in this article.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *