Results 1 to 5 of 5

Thread: [RESOLVED] Convert Date() to Time()

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Resolved [RESOLVED] Convert Date() to Time()

    Hello I have a date in a string formatted like so: "d/m/y-H:i:s" using the date function. I want to convert that specific formatted date to a UTC time. Since I need to be able to compare other dates to this one.

    How do I do this?

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Convert Date() to Time()

    If you used date() to format it, could you not simply save the pre-formatted timestamp in a separate variable?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Convert Date() to Time()

    I thought of that at first but the issue is that a third party program is generating the date format in a text file. I need to read the text file to determine if the date is greater than the current date minus one hour.

    So you see that I have no control over the generated date in the text file. I just need to retrieve the date from the text file and run a few comparisons on it.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Convert Date() to Time()

    if you are using PHP 5.3, you can use date_parse_from_format():

    PHP Code:
    <?php
      
      header
    ("Content-type: text/plain");
      
      
    date_default_timezone_set("America/Denver");
      
      
    $format "d/m/y-H:i:s";
      
      
    $date date_parse_from_format($formatdate($format));
      
      
    $timestamp mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']);
      
      
    print_r($date);
      
      echo 
    $timestamp;
    ?>
    if you are not using PHP 5.3, you'll have to parse the date and use mktime() to convert it to a Unix timestamp, and from there you can use date() to convert it however you'd like. functions like strtotime() won't work because the format your dates are in is unsupported.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Convert Date() to Time()

    Since I'm not using PHP 5.3 I ended up just using substr's, it's not very flexible when it comes to changing the formats but it will suit my current needs.

    Code:
    $format = "d/m/y-H:i:s";
    $date = array();
    $date['day'] = substr($currDate,0,2);
    $date['month'] = substr($currDate,3,2);
    $date['year'] = substr($currDate,6,2);
    $date['hour'] = substr($currDate,9,2);
    $date['minute'] = substr($currDate,12,2);
    $date['second'] = substr($currDate,15,2);
    $currTimestamp = mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']);

Posting Permissions

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



Click Here to Expand Forum to Full Width