Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 22nd April 2010, 6:26 PM   #1
kthnx Thread Starter
Member
 
kthnx's Avatar
 
Join Date: Apr 2006
Location: NSW
Posts: 1,133
Default mktime(); time(); date(); strtotime(); - halp :'(

Hey all,

PHP Code:
$distant "2010-04-28 17:00:00"
What's the best way to tell if $distant is today, tomorrow or later? As far as I can tell (majorly confused) I should be using one of the functions in the title but I can't get my head around how they all interact.

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:
Originally Posted by kevmif
Fucking hell... Batman can get ADSL and i'm stuck on dial up.
eBay FS: null | BakeOff Finalist 06 | Google it | 30+ successful trades for total >$1500
kthnx is offline   Reply With Quote

Join OCAU to remove this ad!
Old 22nd April 2010, 6:54 PM   #2
alexlim2000
Member
 
Join Date: Nov 2006
Posts: 107
Default

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.
alexlim2000 is offline   Reply With Quote
Old 23rd April 2010, 1:27 AM   #3
Ashpool
Member
 
Ashpool's Avatar
 
Join Date: Feb 2003
Location: Ye Olde Melbourne Town
Posts: 3,255
Default

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;
}

?>
Ashpool is offline   Reply With Quote
Old 23rd April 2010, 4:47 PM   #4
Foliage
Member
 
Foliage's Avatar
 
Join Date: Jan 2002
Location: Sleepwithyourdadelaide
Posts: 23,945
Default

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.
Foliage is offline   Reply With Quote
Old 23rd April 2010, 7:55 PM   #5
BIPyjamas
Member
 
BIPyjamas's Avatar
 
Join Date: Jun 2001
Location: Sydney, Australia
Posts: 620
Default

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.
BIPyjamas is offline   Reply With Quote
Old 24th April 2010, 2:48 AM   #6
Ashpool
Member
 
Ashpool's Avatar
 
Join Date: Feb 2003
Location: Ye Olde Melbourne Town
Posts: 3,255
Thumbs up

Quote:
Originally Posted by BIPyjamas View Post
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.
Yes, nice spotting! It was the wrong part.

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.
Ashpool is offline   Reply With Quote
Old 29th April 2010, 12:31 AM   #7
BIPyjamas
Member
 
BIPyjamas's Avatar
 
Join Date: Jun 2001
Location: Sydney, Australia
Posts: 620
Default

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);
That way you get nice easy assignment of date/time parts into singular variables so you can muck about with creating dates and unix times as needed. To each there own as you say.

Last edited by BIPyjamas; 29th April 2010 at 12:35 AM.
BIPyjamas is offline   Reply With Quote
Old 29th April 2010, 1:10 AM   #8
kthnx Thread Starter
Member
 
kthnx's Avatar
 
Join Date: Apr 2006
Location: NSW
Posts: 1,133
Default

Thanks for the responses guys, helped me realise there was a much more elegant way to do it

For the record:

PHP Code:
<?
function printTime($raw)
{
// Finds the current day of the year (0-365) and the event day of the year (0-365)
$currentDay date("z");
$eventDay date("z"strtotime($raw));
// Compensates for if the event is next year and thus on a new 0-365 loop
if ((date("Y"strtotime($raw))) != (date("Y"))) {
        
$eventDay $eventDay 365;
        
// and if it's a leap year, we've got to add one more day
        
if (date("L") == 1) {
            
$eventDay++;
        }
}
// Format box if today
if (($eventDay $currentDay) == 0) {
$boxDay "Today";
$boxColor "red";
}
// If tomorrow
if (($eventDay $currentDay) == 1) {
$boxDay "Tommorow";
$boxColor "yellow";
}
// If within one week
if ((($eventDay $currentDay) >> 1) && (($eventDay $currentDay) << 7)) {
$boxDay date("l"strtotime($raw));
$boxColor "blue";
}
// If over a week
if (($eventDay $currentDay) >= 7) {
$boxDay date("l, j M"strtotime($raw));
$boxColor "blue";    
}
// Prints box
echo "<div class=\"event-date ".$boxColor."\"><span>";
echo 
$boxDay." ".date("g:i a"strtotime($raw));
echo 
"</span></div>";
}
?>
Receives $raw timestamps like 2010-04-29 17:00:00 and formats the display to Today, Tomorrow, Within 1 week, >1week. Works for leap years etc too
__________________
Quote:
Originally Posted by kevmif
Fucking hell... Batman can get ADSL and i'm stuck on dial up.
eBay FS: null | BakeOff Finalist 06 | Google it | 30+ successful trades for total >$1500
kthnx is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 6:24 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!