Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    179
    Thanks
    39
    Thanked 2 Times in 2 Posts

    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 11:18 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    Last edited by Fou-Lu; 02-07-2013 at 07:29 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    nani_nisha06 (02-08-2013)

  • #3
    Regular Coder
    Join Date
    Oct 2012
    Location
    mother land --india
    Posts
    179
    Thanks
    39
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •