简体   繁体   中英

Add 7 days to PHP date and change format at once

I have a date being echoed in my code like so.

date("F j", strtotime($dateEnd))

I want to change it to do something like...

date("+7 day", "F j", strtotime($dateEnd))

But adding the +7 day stops it from working, but in other situations adding days that way has worked. Is it possible to change the format to "F j" and add 7 days at once?

Try this:

  date("F j", strtotime($dateEnd . "+ 7 day"));

Basically you need to adjust the date before converting it to a timestamp and then pass it to date() for formatting.

Something like:

$d = new DateTime( $dateEnd );
$d->modify( '+7 days' );
echo $d->format( 'F j' );

(PHP >= 5.2 )

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