简体   繁体   English

PHP添加日期至今

[英]PHP Add Days To Date

Alright, so I'm working on a licensing system, and I'm very new to working with PHP. 好的,所以我正在开发许可系统,并且对使用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. 顺便说一句,我可以很好地检索天数,它只是以DATE-TIME格式创建时间戳。

Any help/suggestions will greatly be appreciated. 任何帮助/建议将不胜感激。 I've also tried, 我也尝试过

  • date_modify(...) date_modify(...)

This is easy using DateTime() 使用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: 如果您正在运行PHP 5.4+,则可以将其缩短为一种格式:

$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('Ymd H:i:s')) does. time()返回当前的unix时间戳,所有strtotime(date('Ymd H:i:s'))strtotime(date('Ymd H:i:s'))

另一种使用Carbon的划线:

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

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. 确保$expire是int类型的。 This code will return the date when the license expires. 该代码将返回许可证过期的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM