modernmagic
modernmagic

Reputation: 141

php for this week and next

I can get the date of the Monday for the current week, but how do I get it to display "and (the date for the monday of next week)"?

For example, "March 6 and 13, 2017"

This is what I have so far:

<?php echo date('F j, Y',strtotime(date('o-\WW')));?>

Upvotes: 1

Views: 53

Answers (2)

Kevin_Kinsey
Kevin_Kinsey

Reputation: 2300

An exact script:

$this_monday = date("F j", strtotime("Monday this week"));
$next_monday = date("j, Y", strtotime("Monday next week"));

echo $this_monday . " and " . $next_monday;

The problem will be crossing months. If you need to do that, you'll need more logic, perhaps something like:

$this_monday = strtotime("Monday this week");
$next_monday = strtotime("Monday next week");

if (date("F",$this_monday) === date("F",$next_monday)) {

    echo date("F j", $this_monday) . " and " . date("j, Y", $next_monday);

} else {

    echo date("F j", $this_monday) . " and " . date("F j, Y", $next_monday);
}

Someone may come along and suggest using the Datetime class instead, and that might be something to put on your list to learn, as well.

Upvotes: 0

j08691
j08691

Reputation: 207861

You can use the far more readable:

echo date('F j, Y',strtotime('Monday this week'));
echo date('F j, Y',strtotime('Monday next week')); 

Upvotes: 4

Related Questions