The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Issues with strtotime
Discuss Issues with strtotime in the PHP Development forum on Dev Shed. Issues with strtotime PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 22nd, 2012, 05:45 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Location: Florida
Posts: 5
Time spent in forums: 1 h 53 m 25 sec
Reputation Power: 0
|
|
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;
}
}
}
|

December 22nd, 2012, 06:10 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
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();
?>
|

December 22nd, 2012, 06:21 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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.
|

December 22nd, 2012, 06:33 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
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  .
|

December 22nd, 2012, 06:36 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Location: Florida
Posts: 5
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();
?>
|
|

December 22nd, 2012, 06:44 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Location: Florida
Posts: 5
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
|

December 22nd, 2012, 06:59 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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.
|

December 22nd, 2012, 06:59 PM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
No problem, although I would prefer to compare the numeric date-format (just in case!).
|

December 22nd, 2012, 07:13 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Location: Florida
Posts: 5
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.  |
|

December 22nd, 2012, 08:11 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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.
|

December 22nd, 2012, 08:59 PM
|
 |
Lost in code
|
|
|
|
|
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.
|

December 23rd, 2012, 12:27 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Location: Florida
Posts: 5
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  .
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|