#1
  1. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,468
    Rep Power
    653

    Cannot Set DateTime


    I am trying to calculate a date by setting DateTime to my date not the current date. However, no matter what I do, it is always set to the current date. Here are the things I tried:
    PHP Code:
    $date=new DateTime($expire); 
    and
    PHP Code:
    $date=new DateTime();
    $dte=explode("-",$expire);
    $date->setDate($dte[0],$dte[1],$dte[2]); 
    In this case $expire is set to the string "2018-08-14". How do I set the DateTime to that? TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,177
    Rep Power
    2011
    Strange, the second code should be be working. You have tried to print $expire and validated it does have the correct/expected value?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    142
    Rep Power
    66
    I would use var_dump($expire); right before the DateTime() call to see if there's anything that can be learned by looking at the value.

    Also, how are you viewing the incorrect result? The problem could be with that code.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,468
    Rep Power
    653
    Yes, I verified all the values. Here is the full code:
    PHP Code:
    date_default_timezone_set("America/New_York");
    echo 
    $_POST["days"]."<br />";
    $interval=new dateinterval("P".$_POST["days"]."D");
    echo 
    $_POST["expire"]."<br />";
    $dte=explode("-",$expire);
    $date=new DateTime();
    $date->setDate($dte[0],$dte[1],$dte[2]);
    echo 
    "<pre>";
    print(
    var_dump($date));
    echo 
    "</pre>";
    $date->add($interval);
    echo 
    $date->format("Y-m-d"); 
    here is the output:
    Code:
    30
    2018-08-14
    
    object(DateTime)#2 (3) {
      ["date"]=>
      string(19) "2018-07-20 08:45:24"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(16) "America/New_York"
    }
    2018-08-19
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    142
    Rep Power
    66
    For the version where you are using explode(), you should be getting a php warning that explains why it isn't working - Warning: DateTime::setDate() expects parameter 1 to be integer, string given in ... The $_POST data is a string. You need to cast the parts to integers.

    For the posted code, changing it to use - $date=new DateTime($_POST['expire']); and eliminating the setDate() call, works for me, but since the code in the 1st post is using a variable $expire, I would guess that there's some problem with your version of that code that's creating the $expire variable.
  10. #6
  11. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,884
    Rep Power
    9646
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,468
    Rep Power
    653
    I got it. Apparently the setDate method does not let PHP type cast. This is what worked:
    PHP Code:
    $date->setDate((int) $dte[0],(int) $dte[1],(int) $dte[2]); 
    If it doesn't like the parameters it uses today.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,468
    Rep Power
    653
    Originally Posted by requinix
    PHP Code:
    new DateTime($expire
    That was where I started and it didn't work.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,884
    Rep Power
    9646
    Originally Posted by gw1500se
    That was where I started and it didn't work.
    https://3v4l.org/sFQnI

    Then there was something else wrong.

IMN logo majestic logo threadwatch logo seochat tools logo