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 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2013
    Location
    Finland/Estonia
    Posts
    53
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Get Epoch datetime, I have different textfields for date and time

    Okay.. So I got a datepicker in a form. And I also got a timepicker in the form. both are separate fields. I need to somehow add them up and convert them to epoch. What's the easiest spot to do so and how would that be done?

    This is my testing script:
    PHP Code:
    $postDate mysqli_real_escape_string($db$_POST['datepicker']); //date
    $newTime mysqli_real_escape_string($db$_POST['newTime']); //time (eg 21:00)

    $theDate strtotime($postDate); //convert m/d/Y date to epoch, works fine
    $clockToEpoch strtotime($newTime); //tried to do the same on time, returned current time
    echo "clockToEpoch: ".$clockToEpoch;
    echo 
    "<br>Thedate:".$theDate;
    $newerDate $theDate $clockToEpoch//addition, ends up at year 2040-2050
    echo "<br>final date: ".$newerDate;
    exit(); 
    Where and how should I get the date and time into one?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There's no need to escape these. The strtotime or datetime handling will deal with invalid entries from a user.
    Simply concatenate the strings together to make a single string of date and time. Then call strtotime.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

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

    estXcrew (01-22-2014)

  • #3
    New Coder
    Join Date
    Jul 2013
    Location
    Finland/Estonia
    Posts
    53
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    There's no need to escape these. The strtotime or datetime handling will deal with invalid entries from a user.
    Simply concatenate the strings together to make a single string of date and time. Then call strtotime.
    Thank you. This is what I ended up with. I was afraid that the strtotime() function wouldn't be able to use that time.

    PHP Code:
    $postDate $_POST['datepicker'];
    $newTime $_POST['newTime'];
    date_default_timezone_set("UTC"); //set to UTC/GMT
    $theDateTime strtotime($postDate.' '.$newTime); 

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That should work no problem.
    I myself would use the DateTime class, but as an OO programmer I'm more comfortable with that than the strtotime.
    Strtotime's only real issue is its poor results in the past making it difficult to know if it was successful. So long as your in 5.1+ than you can simply check against false. Otherwise, its -1, and that's where the complication comes in (since -1 is a valid time).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Jul 2013
    Location
    Finland/Estonia
    Posts
    53
    Thanks
    9
    Thanked 0 Times in 0 Posts
    You mean using the <input type="datetime">?
    I'm not using that because I think only 1 or 2 browsers support them, not speaking about older versions.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No I mean datetime object oriented code:
    PHP Code:
    $dt = new DateTime($postDate.' '.$newTime); 
    $dt will be an object of type datetime. I can manipulate it the same way as anything else, but on an invalid datetime string provided it will throw an Exception so I can always tell if the date/time provided is valid.

    Input's from forms are always strings. The type provided is for client only; text will be a text box, passwords are replaced with the *'s, and the new HTML5 stuff will either provide selectable inputs (I don't know I don't do HTML work I'm afraid), or perhaps provide a simple format check to indicate its invalid. Whatever the HTML5 does, when its down to submission its still just a string.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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