简体   繁体   中英

How to substract a String time from the current date?

I'd like to substract a specified time from the current date. I don't know why but it says that my date (which I want to sub) is a string. How can I convert it to a format which if I substract the current time from, I get the days left value.

Thank you in advance!

Assuming you have your date in a variable named $dateToSubtract , you can use PHP's strtotime() to convert it to a timestamp:

$timestamp = strtotime($dateToSubtract);

To use in finding the difference between "now" and the specified date:

$date = time() - strtotime($dateToSubtract);

Use the dateDiff function specified HERE . It's the most effective thing I have found.

Suppose $num_of_days is the no. of days you want to subtract from current day.

 $new_date =  date('Ymd', strtotime(date('Ymd', strtotime(date('Ymd') . " -  ".$num_of_days."days"))));

$new_date would be in date Ymd format. If you want it in time,

 $new_time = strtotime(date('Ymd', strtotime(date('Ymd') . " -  ".$num_of_days."days")));

strtotime (按字面名称命名为“时间字符串”)应该是您要寻找的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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