Inserting data and time and calculating the time left

So I have to columns one is date registered and date of expire.

For date registered I use the code below

[php]
date(“Y-m-d H:i:s”);
[/php]

My radio buttons have :
24h expiry date
2days
4days

How do you suggest I do this ?

While I think I understand what you want, clearly laying out the issue is helpful going forward along with, what you have tried to do to solve the problem.

You want to look into date modify

Here are a few ways that do the same thing.
[php]function findExpireDate( $interval )
{
$interval = intval($interval);
$regDate = date(‘Y-m-d H:i:s’);
$date = new DateTime($regDate);
// possibilities [ 24, 48, 96 ]
switch ($interval) {
case 24: // 1 day
$date->modify("+24 hours");
break;
case 48:
$date->modify("+48 hours");
break;
case 96:
$date->modify("+96 hours");
break;
default:
$date;
}
return $date->format(‘Y-m-d H:i:s’);
}

function cleanExpDate($interval, $startDate){
$allowable = [
24,
48,
96,
];

if( !in_array($interval, $allowable))
    return false;
$date = new DateTime($startDate);
$date->modify("+$interval hours");
return $date->format('Y-m-d H:i:s');

}

echo “

” . date(‘Y-m-d H:i:s’) . “

”;
echo "

findExpireDate: " . findExpireDate(24) . “

”;
$clean = cleanExpDate(24, date(‘Y-m-d H:i:s’));
if ( $clean != false ){
echo “

$clean

”;
} else {
echo “

Date was in improper format.

”;
}
[/php]

Here’s a way in using date_diff http://php.net/manual/en/function.date-diff.php and date modify:
[php]<?php
/*

  • $days_to_expiration would be the variable that would
  • get the number of days till expiration.
    */
    $days_to_expiration = 4;
    $register_date = “2016-03-05 12:15:22”;

/*

  • Function to calculate expiration date
    */
    function expiration_date($days_to_expiration) {
    $expiriation_date = new DateTime(“NOW " . $days_to_expiration . " days”, new DateTimeZone(“America/Detroit”));
    $expiriation_date->modify(“Midnight”);
    //echo “
    ” . print_r($expiriation_date, 1) . “
    \n”;
    $exp_date = $expiriation_date->format(“Y-m-d H:i:s”);
    return $exp_date;
    }

$exp_date = expiration_date($days_to_expiration);

/*

  • Function to check expiration date itself
    /
    function check_expiration($expire) {
    $today = new DateTime(“NOW”, new DateTimeZone(“America/Detroit”));
    $today->modify(“Today Midnight”); // Today’s date at midnight:
    $expiration_date = new DateTime($expire, new DateTimeZone(“America/Detroit”));
    $diff = $expiration_date->diff($today);
    //echo “
    ” . print_r($diff, 1) . “
    \n”;
    /
    • If date is yesterday and past today’s date then it is
    • expired, plus invert equal 0, so return the result back.
      /
      if ($diff->invert === 0 && $diff->d > 0) {
      return “Sorry Expiration Date!
      \n”;
      }
      /
    • If invert is zero and day is zero then it
    • is the day that is expiring so return result.
      */
      if ($diff->invert === 0 && $diff->d === 0) {
      return “Account is expiring today!”;
      }
      switch ($diff->d) {
      case 1: // 1 day
      return “1 day to expiration!
      \n”;
      break;
      case 2:
      return “2 days to expiration!
      \n”;
      break;
      case 3:
      return “3 days to expiration!
      \n”;
      break;
      case 4:
      return “4 days to expiration!
      \n”;
      break;
      default:
      return “undetermined”;
      }
      }

$status = check_expiration($exp_date);
echo $status;
[/php]

Of course with a few tweaks this could refined even more where you can calculate hours and what have you.

Sponsor our Newsletter | Privacy Policy | Terms of Service