simPod
simPod

Reputation: 13456

PHP Datetime `next week wednesday` gives me monday

I'm trying to get the next week's Wednesday date using new \DateTime('next week wednesday'). However, it returns 2016-12-19 00:00:00.000000 which is Monday. Why is it so? How to get correct result?

I tried to reproduce it in a PHP sandbox online but there it returns correct result http://sandbox.onlinephpfunctions.com/code/7ab99fcfeffedc1ad01d7de9ed236ac273fe1bb3 Can it be something depending on my environment?

I'm running PHP 7 on OSX

PHP 7.0.11 (cli) (built: Oct 2 2016 00:32:59) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

enter image description here

Upvotes: 3

Views: 1241

Answers (2)

Bernard van der Velden
Bernard van der Velden

Reputation: 113

I get the same results here, indeed seems to be an 'next week' thingy:

$date = new \DateTime('next week wednesday');
print_r($date);

DateTime Object
(
    [date] => 2016-12-19 00:00:00.000000
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)

Rewriting seems to work, depending on what you want:

$date = new \DateTime('wednesday');
print_r($date);

DateTime Object
(
    [date] => 2016-12-21 00:00:00.000000
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)

or

$date = new \DateTime('wednesday');
$date->add(new \DateInterval('P1W'));
print_r($date);

DateTime Object
(
    [date] => 2016-12-28 00:00:00.000000
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)

Upvotes: 0

Chris
Chris

Reputation: 136948

This appears to be a bug (possibly this one that only occurs on Sundays):

<?php

var_dump(new \DateTime('Wednesday next week'));

PHP versions 5.6.23 to 5.6.29 and 7.0.8 to 7.1.0 output December 19 (a Monday), while PHP versions 5.0 to 5.6.22 and 7.0.0 to 7.0.7 return December 28 (a Wednesday).

I don't see any mention of related changes in the changelog entries for PHP 5.6.23 or 7.0.8.

Upvotes: 5

Related Questions