www.webdeveloper.com
Results 1 to 6 of 6

Thread: Help with Date Time Conversions

  1. #1
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    363

    Question Help with Date Time Conversions

    I'm trying to convert my date and time to a better format so I can compare it to a time and date 36 hours from now. Then I plan to compare these two so I can see which date is earlier.

    My problem is that I can't just use the typical methods because the code is mixing my day and month.

    Here's my code:

    PHP Code:
    $orig$v_CurrentRecordD['day'].",  ".$v_CurrentRecordD['time']; //MM/DD/YYYY, HH:MM
    $conv date('m/d/Y H:i'strtotime($orig));
    $daycutoff date('m/d/Y H:i'strtotime("+36 hours"));
    echo 
    $orig."-".$conv."-".$daycutoff." <br />"
    Here are my results:
    HTML Code:
    06-20-2013, 8:00am-12/31/1969 18:00-06/05/2013 22:56
    02-11-2013, 8:45am-11/02/2013 08:45-06/05/2013 22:56
    02-11-2013, 8:15am-11/02/2013 08:15-06/05/2013 22:56
    02-11-2013, 8:00am-11/02/2013 08:00-06/05/2013 22:56
    06-01-2012, 10:15am-01/06/2012 10:15-06/05/2013 22:56
    02-11-2013, 8:30am-11/02/2013 08:30-06/05/2013 22:56
    02-12-2013, 10:45am-12/02/2013 10:45-06/05/2013 22:56
    02-12-2013, 10:30am-12/02/2013 10:30-06/05/2013 22:56 
    Help, anyone?

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    I think the key here is to do your "date-time arithmetic" in the UNIX timestamp domain (which is an integer), not the formatted date string domain. So, just use the integer values returned by strtotime() (without processing them back into a string with date()) to do your comparisons, then just use date() in your output to make the results human-readable again.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    363
    Thanks, NogDog, but my problem is more the initial conversions than the arithmetic. The first two "columns" of my output are a little messed up because of the date and time format. The third "column" is today's date and time +36 hours, which seems to be working fine. (Sorry, I could've simplified it.)

    Something is definitely messed up where I think it's mixing up days and months, and also there are some anomalies like in the first row.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    Sounds like it might be a case where you might want to use 3 separate input fields for date, so that there is no confusion between the fact that, in general, 5/7/2013 is the same thing as 7-5-2013 for strtotime().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    363
    Thanks, but I can't change the variables.
    ...or can I? Is there a way to do that?
    This string is the one I have to use, but if I can modify it into three variables, that would work.

    Can that be done? How?

  6. #6
    Join Date
    Dec 2012
    Posts
    95
    You can split that date string into the individual numbers using the explode function

    PHP Code:
    $dateParts explode("-",$v_CurrentRecordD['day']);
    $month $dateParts[0];
    $day $dateParts[1];
    $year $dateParts[2];

    $timestamp strtotime($year."-".$month."-".$day); 
    Last edited by iBeZi; 06-12-2013 at 12:19 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles