I have a date time and now I want to have a automated code to add +5 weekdays only to that datetime constrain so as a try I have used below code but it is adding 5 weekdays from the date but echoing time as zero's.
These are *not* datetime. These are strings and integers, don't mistake the two as the DateTime is an object type, not a scalar type.
This is your order of evaluation of relative versus non-relative formats:
PHP Code:
$constructed = strtotime("2013-02-06 17:59:19"); $date = date('Y:m:d H:i:s', $constructed); $final=date('Y:m:d H:i:s',strtotime("$date + 5 weekdays")); print $final . PHP_EOL;// 2013:02:13 00:00:00 (which is correct for the criteria)
$final = date('Y:m:d H:i:s', strtotime("+5 weekdays $date")); print $final . PHP_EOL; // 2013:02:13 17:59:19 (which is correct for the criteria)
Edit:
In hindsight, this may be a bug. The relative should always apply *after* the absolute, unless its 'yesterday', 'midnight', 'today', 'noon', or 'tomorrow'. So that says that the above should work in either order which it certainly does not appear to.
I'd simply use the DateTime myself:
PHP Code:
$dt = new DateTime('2013-02-06 17:59:19'); $dt->add(DateInterval::createFromDateString('+5 weekday')); print $dt->format('F j Y H:i:s');
Which gives me 'February 13 2013, 17:59:19' as the result.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
These are *not* datetime. These are strings and integers, don't mistake the two as the DateTime is an object type, not a scalar type.
This is your order of evaluation of relative versus non-relative formats:
PHP Code:
$constructed = strtotime("2013-02-06 17:59:19"); $date = date('Y:m:d H:i:s', $constructed); $final=date('Y:m:d H:i:s',strtotime("$date + 5 weekdays")); print $final . PHP_EOL;// 2013:02:13 00:00:00 (which is correct for the criteria)
$final = date('Y:m:d H:i:s', strtotime("+5 weekdays $date")); print $final . PHP_EOL; // 2013:02:13 17:59:19 (which is correct for the criteria)
Edit:
In hindsight, this may be a bug. The relative should always apply *after* the absolute, unless its 'yesterday', 'midnight', 'today', 'noon', or 'tomorrow'. So that says that the above should work in either order which it certainly does not appear to.
I'd simply use the DateTime myself:
PHP Code:
$dt = new DateTime('2013-02-06 17:59:19'); $dt->add(DateInterval::createFromDateString('+5 weekday')); print $dt->format('F j Y H:i:s');
Which gives me 'February 13 2013, 17:59:19' as the result.
Fou-Lu,
One thing I don't understand below comments can you brief.
Code:
These are *not* datetime. These are strings and integers, don't mistake the two as the DateTime is an object type, not a scalar type.