#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Florida
    Posts
    5
    Rep Power
    0

    Question Issues with strtotime


    I'm pretty new to PHP and so far i've written a function to echo the next game in a schedule. Problem is, I want to display "Today" if there is a game today. Currently if today's date = $today, nothing gets displayed . I've tried an elseif statement inside the foreach, but with no results.

    If anyone could point me in the right direction, that be great. Thanks.

    Code:
    function next_game(){
    	$schedule = array(
    		'vs. team1 '		=> strtotime('17 Dec 2012'),
    		'vs. team2 '		=> strtotime('19 Dec 2012'),
    		'@team3 '		=> strtotime('22 Dec 2012'),
    		'@team4 '		=> strtotime('4 Jan 2012')
    	); 
    
    	$today = time();// current timestamp
    
    	foreach($schedule as $place => $date){
    		if($date > $today) {
    			echo date('M dS', $date)."<small>".$place."</small>";
    			break;
    		}
    	}
    }
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    You haven't specified a time with your dates, but time() will include the time, so if you compare the two they won't match.

    time()
    Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
    The best (easiest) way to compare them is to convert them both to the same string-formats without the time element. The following code uses echoes so that you can see what is happening.
    PHP Code:
    <?php
    function next_game(){
        
    $schedule = array(
            
    'vs. team1 '        => strtotime('17 Dec 2012'),
            
    'vs. team2 '        => strtotime('19 Dec 2012'),
            
    '@team3 '       => strtotime('23 Dec 2012'),
            
    '@team4 '       => strtotime('4 Jan 2013')
        ); 

        
    $today time();// current timestamp

        
    foreach($schedule as $place => $date){
            echo 
    "Raw: $date$today<br>";
            echo  
    "Formatted: " date("Ymd"$date). ", " date("Ymd"$today) . "<br>";
            
            if (
    date("Ymd"$date) == date("Ymd"$today)) {
                echo 
    date('M dS'$date)."<small>".$place."</small><br>";
            } else if(
    $date $today) {
                echo 
    date('M dS'$date)."<small>".$place."</small><br>";
                break;
            }
        }
    }
    next_game();
    ?>
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    personally, I don't like the idea of "misusing" format strings for date comparisons. I'd rather use date_parse() to compare the year, month and day as actual numbers.
  6. #4
  7. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Originally Posted by Jacques1
    Hi,

    personally, I don't like the idea of "misusing" format strings for date comparisons. I'd rather use date_parse() to compare the year, month and day as actual numbers.
    Yes, creating dates from strings and then converting them back to strings in order to compare them might be considered a bit of a hack .
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Florida
    Posts
    5
    Rep Power
    0
    Andrew, with your help I saw what my issue was. I didn't format $today correctly to compare;

    And I personally like this method. It's a bit cleaner and clearer.

    original:
    Code:
    if($date > $today) {
    updated and working:
    Code:
    if (date("M dS", $date) == date("M dS", $today)) {
    Thanks, this works how I intended.
    Originally Posted by AndrewSW
    You haven't specified a time with your dates, but time() will include the time, so if you compare the two they won't match.

    time()

    The best (easiest) way to compare them is to convert them both to the same string-formats without the time element. The following code uses echoes so that you can see what is happening.
    PHP Code:
    <?php
    function next_game(){
        
    $schedule = array(
            
    'vs. team1 '        => strtotime('17 Dec 2012'),
            
    'vs. team2 '        => strtotime('19 Dec 2012'),
            
    '@team3 '       => strtotime('23 Dec 2012'),
            
    '@team4 '       => strtotime('4 Jan 2013')
        ); 

        
    $today time();// current timestamp

        
    foreach($schedule as $place => $date){
            echo 
    "Raw: $date$today<br>";
            echo  
    "Formatted: " date("Ymd"$date). ", " date("Ymd"$today) . "<br>";
            
            if (
    date("Ymd"$date) == date("Ymd"$today)) {
                echo 
    date('M dS'$date)."<small>".$place."</small><br>";
            } else if(
    $date $today) {
                echo 
    date('M dS'$date)."<small>".$place."</small><br>";
                break;
            }
        }
    }
    next_game();
    ?>
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Florida
    Posts
    5
    Rep Power
    0
    Oh I forgot...here's what I ended up going with:

    Code:
    function next_game(){ 
      $schedule = array( 
        'vs. team1 ' => strtotime('17 Dec 2012'), 
        'vs. team2 ' => strtotime('19 Dec 2012'), 
        '@team3 ' => strtotime('23 Dec 2012'), 
        '@team4 ' => strtotime('4 Jan 2013') 
      ); 
    
      $today = time();// current timestamp 
      foreach($schedule as $place => $date){ 
        if (date("M dS", $date) == date("M dS", $today)) { 
          echo "Today <small>".$place."</small>";break; 
          } 
        else if($date > $today) { 
          echo date('M dS', $date)."<small>".$place."</small>"; 
          break; 
        } 
      } 
    }
    Thanks guys
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    The format string "M dS" is wrong (there is no year), and it makes no sense whatsoever. Why would you compare date strings like "Jan 01st"??

    I mean, AndrewSW's solution at least compares digits, but yours is like comparing numbers by converting them into their english name (3000 + 24 == 3024, because "Three thousand twenty-four" == "Three thousand twenty-four").

    ... which makes the "clearer and cleaner" sound a bit funny.
    Last edited by Jacques1; December 22nd, 2012 at 07:04 PM.
  14. #8
  15. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    No problem, although I would prefer to compare the numeric date-format (just in case!).
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Florida
    Posts
    5
    Rep Power
    0
    I'm a designer trying to learn code...so i'm trying to easiet way i've learned so far, and as a designer i feel it isn't necessary to write "next game: January 1st, 2013" if its on the 2012-1013 calandar. It looks best and more simple just to write "next game: Jan. 1st."

    Originally Posted by Jacques1
    The format string "M dS" is wrong (there is no year), and it makes no sense whatsoever. Why would you compare date strings like "Jan 01st"??

    I mean, AndrewSW's solution at least compares digits, but yours is like comparing numbers by converting them into their english name (3000 + 24 == 3024, because "Three thousand twenty-four" == "Three thousand twenty-four").

    ... which makes the "clearer and cleaner" sound a bit funny.
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    You're confusing two different things. When it comes to displaying dates, then it's absolutely correct to use the date() function and "M dS". That's exactly what they're are meant for.

    But you're using them to compare dates. According to your logic, today (2012-12-22) equals 2050-12-22, because "Dec 22nd" == "Dec 22nd".

    You don't accound for the year. But even if you did, using english month names to compare date information makes absolutely no sense. It's extremely fragile, inefficient and just odd. For example, are "December", "december" and "Dec" three different months because of the different string representations?

    I mean, you can use this solution if you think it works fine. But from a technical perspective, it's simply wrong. And obviously it confused you. So if you want a clean solution, I'd reconsider using date_parse() or getdate().

    This is how I'd do it:
    PHP Code:
    $compare getdate(strtotime('22 Dec 2012'));
    $today getdate();

    if ( 
    $today['year'] == $compare['year'] && $today['yday'] == $compare['yday'] ) {    // compare years and day of the year
        
    echo "today is: 22 Dec 2012";

    Yeah, it's a few characters longer. But it's simple, comprehensible (no need to look up the date formats), and it works.
    Last edited by Jacques1; December 22nd, 2012 at 08:17 PM.
  20. #11
  21. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    When you're working with dates, the format date("Ymd") is a comparable and sortable integer; that's what I recommend using.

    date("YmdHis") is comparable and sortable too, but can only be treated as integer in 64-bit PHP. If you need to include time too then you are better off using the unix timestamp.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Florida
    Posts
    5
    Rep Power
    0
    Originally Posted by Jacques1

    But you're using them to compare dates. According to your logic, today (2012-12-22) equals 2050-12-22, because "Dec 22nd" == "Dec 22nd".

    I mean, you can use this solution if you think it works fine. But from a technical perspective, it's simply wrong. And obviously it confused you. So if you want a clean solution, I'd reconsider using date_parse() or getdate().

    This is how I'd do it:
    PHP Code:
    $compare getdate(strtotime('22 Dec 2012'));
    $today getdate();

    if ( 
    $today['year'] == $compare['year'] && $today['yday'] == $compare['yday'] ) {    // compare years and day of the year
        
    echo "today is: 22 Dec 2012";

    All points considered, thanks. i'll look into it. Like I said i just pick this language up not too long ago. What I'm trying to accomplish here is to let viewers see when the next game is being played, and against what team, which is why i set my array up that way. After the 2012-2013 season, that section would be re-purposed until the 2013-14 season...Not good to assume but I pretty sure if im looking at a 2012-13 basketball calandar, Dec 23rd ,means Dec 23, 2012

    I'm trying to filter through the schedule(array) and compare those dates to today, or the next game, and echo the result. But seeing that this isn't the best method, i will do more research on this .

IMN logo majestic logo threadwatch logo seochat tools logo