PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 22nd, 2012, 05:45 PM
BigEL BigEL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Florida
Posts: 5 BigEL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 25 sec
Reputation Power: 0
Question PHP-General - 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;
		}
	}
}

Reply With Quote
  #2  
Old December 22nd, 2012, 06:10 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
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()
Quote:
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();
?>

Reply With Quote
  #3  
Old December 22nd, 2012, 06:21 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 2 m 34 sec
Reputation Power: 813
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.

Reply With Quote
  #4  
Old December 22nd, 2012, 06:33 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
Quote:
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 .

Reply With Quote
  #5  
Old December 22nd, 2012, 06:36 PM
BigEL BigEL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Florida
Posts: 5 BigEL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 25 sec
Reputation 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.
Quote:
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();
?>

Reply With Quote
  #6  
Old December 22nd, 2012, 06:44 PM
BigEL BigEL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Florida
Posts: 5 BigEL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 25 sec
Reputation 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

Reply With Quote
  #7  
Old December 22nd, 2012, 06:59 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 2 m 34 sec
Reputation Power: 813
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.

Reply With Quote
  #8  
Old December 22nd, 2012, 06:59 PM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 23 h 1 m 13 sec
Reputation Power: 164
No problem, although I would prefer to compare the numeric date-format (just in case!).

Reply With Quote
  #9  
Old December 22nd, 2012, 07:13 PM
BigEL BigEL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Florida
Posts: 5 BigEL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 25 sec
Reputation 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."

Quote:
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.

Reply With Quote
  #10  
Old December 22nd, 2012, 08:11 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 2 m 34 sec
Reputation Power: 813
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.

Reply With Quote
  #11  
Old December 22nd, 2012, 08:59 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,944 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 16 m 54 sec
Reputation Power: 7053
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
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
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

Reply With Quote
  #12  
Old December 23rd, 2012, 12:27 PM
BigEL BigEL is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Florida
Posts: 5 BigEL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 25 sec
Reputation Power: 0
Quote:
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 .

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Issues with strtotime

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap