AV1611 Posted April 15, 2007 Share Posted April 15, 2007 I have a database that returns a field with a date and time that looks like this: 12/04/2007 07:45:03 I need a simple clause that says if the time is before noon then A else if the time is after noon then B I could split the date from the time in a string then split the time by the : but there is a much easier way, right? thanks Link to comment https://forums.phpfreaks.com/topic/47107-solved-clause-based-on-datetime/ Share on other sites More sharing options...
kenrbnsn Posted April 15, 2007 Share Posted April 15, 2007 I would use a combination of the strtotime() and date() functions: <?php $date_str = '12/04/2007 07:45:03'; $hour = date('g',strtotime($date_str)); if ($hour < 12) echo 'Before noon'; else echo 'After noon'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/47107-solved-clause-based-on-datetime/#findComment-229711 Share on other sites More sharing options...
AV1611 Posted April 15, 2007 Author Share Posted April 15, 2007 You'd think I'd know how to do that by now... :'( Link to comment https://forums.phpfreaks.com/topic/47107-solved-clause-based-on-datetime/#findComment-229713 Share on other sites More sharing options...
AV1611 Posted April 15, 2007 Author Share Posted April 15, 2007 Still not working ??? it works but quits after a few rows: datafield --> what is produced 10/04/2007 07:14:39 -->7 10/04/2007 16:07:14 -->16 11/04/2007 07:13:54 -->7 11/04/2007 17:01:20 -->17 12/04/2007 07:45:03 -->7 12/04/2007 17:00:17 -->17 13/04/2007 09:25:54 -->16 13/04/2007 17:10:24 -->16 14/04/2007 08:18:06 -->16 14/04/2007 18:24:03 -->16 15/04/2007 07:37:01 -->16 Here is my script ( I used G not g in the date()...) mysql_select_db($database); $result = mysql_query("Select * from glucose")or die('Died: '.mysql_error()); while($row=mysql_fetch_array($result)){ $date_str = $row[1]; $hour = date('G',strtotime($date_str)); echo $row[1]." -->".$hour." <br/>"; if ($hour <= 11) {echo "<tr><td class='e'>".$row[0]."</td><td class='v'>".$row[1]."</td><td class='e'>".$row[2]."</td><td class='v'>".$row[3]."</td></tr>";} else {echo "<tr><td class='e'>".$row[0]."</td><td class='v2'>".$row[1]."</td><td class='e'>".$row[2]."</td><td class='v'>".$row[3]."</td></tr>";} } echo "</table></center>"; Link to comment https://forums.phpfreaks.com/topic/47107-solved-clause-based-on-datetime/#findComment-229723 Share on other sites More sharing options...
AV1611 Posted April 15, 2007 Author Share Posted April 15, 2007 Well, I fixed it by doing this but can someone tell me why it didn't just work before? fix: while($row=mysql_fetch_array($result)){ $date_str = $row[1]; $fin=explode(' ',$date_str); $date_str = $fin[1]; $hour = date('G',strtotime($date_str)); Link to comment https://forums.phpfreaks.com/topic/47107-solved-clause-based-on-datetime/#findComment-229729 Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 strtotime can be finicky about string formats. If it were properly formatted as "2007-04-14 07:37:01" then you would've been OK Link to comment https://forums.phpfreaks.com/topic/47107-solved-clause-based-on-datetime/#findComment-229851 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.