pee2pee
pee2pee

Reputation: 3792

PHP - Getting next thursday's date

Code

$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date']);
$order_dispatch_date = $order_dispatch_date->format('Ymd');

$order_collection = date('Ymd', strtotime('next thursday', $order_dispatch_date));

$order_collection = new DateTime($order_collection); 

echo '['.$order_collection->format('d/m/Y') . ' - ' . $order_dispatch_date . ' - ' . gettype($order_dispatch_date) . ' ] ';

I get the following output if the input is: 20171128

[29/08/1970 - 20171128 - string ] 

My question is why is the first output showing 29th August 1970 rather than 20th November 2017?

Upvotes: 0

Views: 169

Answers (2)

iainn
iainn

Reputation: 17417

Stop changing things back-and-forth between objects, strings and timestamps so much, especially when you're re-using the same variables. It's needlessly hard to follow.

If you've got a Ymd formatted string, and you want an equivalent string back for the proceeding Thursday, just use this:

$inc['order_dispatch_date'] = '20171128';

$order_dispatch_date = DateTime::createFromFormat('Ymd', $inc['order_dispatch_date'])->modify('next thursday');

echo $order_dispatch_date->format('Ymd');
// 20171130

See https://eval.in/909998

Upvotes: 1

DevionNL
DevionNL

Reputation: 153

Basically:

$order_collection = date('Ymd', strtotime('next thursday', strtotime($order_dispatch_date)));

And that should work :)

Upvotes: 0

Related Questions