Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-07-2013, 05:25 PM   PM User | #1
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
datetime with 5 weekdays date function

Hi frnds,

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.

PHP Code:
$date date('Y:m:d H:i:s'time());
$final=date('Y:m:d H:i:s',strtotime("$date + 5 weekdays")); 
for example:

below is the date I want to add 5 working days to it, I used above code
Code:
2013:02:06 17:59:19
output I am getting as

Code:
2013:02:13 00:00:00
required out put should be

Code:
2013:02:13 17:59:19
Please can any one help me solve this issue.

Regards,
Nani

Last edited by nani_nisha06; 02-08-2013 at 10:18 AM..
nani_nisha06 is offline   Reply With Quote
Old 02-07-2013, 06:09 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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

Last edited by Fou-Lu; 02-07-2013 at 06:29 PM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
nani_nisha06 (02-08-2013)
Old 02-08-2013, 10:19 AM   PM User | #3
nani_nisha06
Regular Coder

 
Join Date: Oct 2012
Location: mother land --india
Posts: 159
Thanks: 37
Thanked 2 Times in 2 Posts
nani_nisha06 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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.
nani_nisha06 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:00 PM.


Advertisement
Log in to turn off these ads.