1

Alright, so I'm working on a licensing system, and I'm very new to working with PHP. I'd like to retrieve the number of days a license key has, then add that to the current day. My code,

$expire = $tags['exp_time']; //EXPIRATION_DATE_DAYS
$expDate = date('Y-m-d H:i:s');
$expDate = strtotime($expDate);
$expDate = $expDate + ((24 * 60 * 60)*($expire));
$expDate = date('Y-m-d H:i:s', $expDate);

I can retrieve the number of days just fine by the way, it's just creating the timestamp in a DATE-TIME format.

Any help/suggestions will greatly be appreciated. I've also tried,

  • date_modify(...)

3 Answers 3

2

This is easy using DateTime()

$date = new DateTime();                  // Create datetime object representing now
$date->modify("+ $expires days");        // Add $expires days to it
$expDate = $date->format("Y-m-d H:i:s"); // Format it

If you're running PHP 5.4+ you can shorten it to a one-liner:

$expDate = (new DateTime())->modify("+ $expires days")->format("Y-m-d H:i:s");

FYI, this code:

$expDate = date('Y-m-d H:i:s');
$expDate = strtotime($expDate);

can be written as:

$expDate = time();

time() returns the current unix timestamp which is all strtotime(date('Y-m-d H:i:s')) does.

Sign up to request clarification or add additional context in comments.

Comments

2

Another one-liner, using Carbon:

$expDate = Carbon::now()->addDays($expire)->toDateTimeString();

Comments

0

You can add days to a date using this code:

$ExpDate = date("Y-m-d g:i:s", strtotime("+".$expire." day", date("Y-m-d g:i:s")));

Make sure $expire is a of type int. This code will return the date when the license expires.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.