![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 | |
|
Member
Join Date: Apr 2006
Location: NSW
Posts: 1,133
|
Hey all,
PHP Code:
I want to be able to tell the user if that datestamp is today (before midnight) tomorrow (before midnight tomorrow, after midnight tonight) or later (after midnight tomorrow) but that needs so many variables I don't know how to construct or operate on ![]() Thanks for any advice! Apologies for being a noob
__________________
Quote:
eBay FS: null | BakeOff Finalist 06 | Google it | 30+ successful trades for total >$1500
|
|
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Nov 2006
Posts: 107
|
Wait, what?
Tomorrow, by definition is after midnight (today/tomorrow midnight). Just check the 'day' component in the date. against today. (or the entire date really). Also; you will need to convert for GMT +10 or whatever timezone you're in. |
|
|
|
|
|
#3 |
|
Member
Join Date: Feb 2003
Location: Ye Olde Melbourne Town
Posts: 3,255
|
Something like this? you probably don't need the cast. I haven't tested it, but you should be able to figure it out from here.
Code:
<?php
$distant = "2010-04-28 17:00:00";
$distantDay = (int) substr($distant, 5, 2);
$todayInt = (int) date("d");
switch ($distantDay) {
case ($distantDay = todayInt):
echo "Today";
break;
case ($distantDay = todayInt + 1):
echo "Tomorrow";
break;
case ($distantDay > todayInt + 1 ):
echo "Some day after tomorrow";
break;
}
?>
|
|
|
|
|
|
#4 |
|
Member
Join Date: Jan 2002
Location: Sleepwithyourdadelaide
Posts: 23,945
|
You can convert a date to an int just like that?
__________________
You know, if you watch Titanic backwards, it's actually a heart warming tale of a ship that jumps out of the water and saves lots of drowning people. |
|
|
|
|
|
#5 |
|
Member
Join Date: Jun 2001
Location: Sydney, Australia
Posts: 620
|
Ashpool didn't convert it to an int, he did a substring to get a part of the date string... which I think is the wrong part. Looks like the month is getting stored rather than the day as intended.
The trouble with doing it that way is that on month boundaries you're going to run into issues. You really need to also check the month as well as the year for end of year boundaries. Last edited by BIPyjamas; 23rd April 2010 at 7:58 PM. |
|
|
|
|
|
#6 | |
|
Member
Join Date: Feb 2003
Location: Ye Olde Melbourne Town
Posts: 3,255
|
Quote:
What I've shown is not a great way to do it but its a good primer to start with. A better way would be to convert the date to an epoch date and do the comparison that way. The standard unix way is measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Note that mktime uses mm-dd-yyyy formats. This way you avoid any problems with month, year rollovers. Code:
<?php
$distant = "2010-04-28 17:00:00";
$distantEpTime = mktime(
0,0,0,
(int) substr($distant, 5, 2),
(int) substr($distant, 8, 2),
(int) substr($distant, 0, 4)
);
$todayEpDate = mktime (
0,0,0,
date("m"),
date("d"),
date("Y")
);
$today = date("Y-m-d");
$tomorrowEpDate = mktime(
0,0,0,
date("m", strtotime($today)),
date("d", strtotime($today)),
date("Y", strtotime($today))
);
$tomorrowEpDate = $tomorrowEpDate + 86400;
switch ($distantEpTime) {
case ($distantEpTime < $todayEpDate):
echo "I think this day has come and gone :";
echo $distantEpTime;
echo " < ";
echo $todayEpDate;
break;
case ($distantEpTime == $todayEpDate):
echo "Today :";
echo $distantEpTime;
echo " = ";
echo $todayEpDate;
break;
case ($distantEpTime == $tomorrowEpDate):
echo "Tomorrow :";
echo $distantEpTime;
echo " = ";
echo $tomorrowEpDate;
break;
case ($distantEpTime > $tomorrowEpDate):
echo "Some day after tomorrow :";
echo $distantEpTime;
echo " > ";
echo $tomorrowEpDate;
break;
}
?>
I'm sure there is a better way to do it but as with all programming there are several ways to skin a cat. In fact thinking about it now you could just compare the mktimes using datediff or the like and use a +1 on the mktime day. MKtime is smart enough to calculate the correct dates if you add numbers past an end of month/year rollover. Last edited by Ashpool; 24th April 2010 at 2:52 AM. |
|
|
|
|
|
|
#7 |
|
Member
Join Date: Jun 2001
Location: Sydney, Australia
Posts: 620
|
I think your initial way was an interesting way of doing it, just wanted to point out the missing bits just incase the OP decides to run with it.
Personally, I prefer to use split() when possible as opposed to substr(). It's not fun keeping track of substring positions; much nicer to use delimiters. For example, Code:
$distant = "2010-04-29 00:24";
list($date, $time) = split(" ", $distant);
list($year, $month, $day) = split("-", $date);
list($hour, $minute) = split(":", $time);
$distantEpTime = mktime(0, 0, 0, $month, $day, $year);
Last edited by BIPyjamas; 29th April 2010 at 12:35 AM. |
|
|
|
|
|
#8 | |
|
Member
Join Date: Apr 2006
Location: NSW
Posts: 1,133
|
Thanks for the responses guys, helped me realise there was a much more elegant way to do it
![]() For the record: PHP Code:
__________________
Quote:
eBay FS: null | BakeOff Finalist 06 | Google it | 30+ successful trades for total >$1500
|
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|