RealDrift Posted June 24, 2008 Share Posted June 24, 2008 Hi, I have a script which displays the date a transaction ocurred in rows, currently it only displays the date and time, what i want it to do: - if the current day is the same as the transaction date then i want it to echo the word "Today" plus the time instead of the date and time - If the transaction happened yesterday i want it to echo the word "yesterday" and time instead of the date and time - if the date is two days before the current date then it shud only display the standard date and time I found a generic script for this: <? $today = date(”m.d.Y”); $yesterday = date(”m.d.Y”, strtotime(”-1 day”)); $mtdate = “<$MTEntryDate format=”%m.%d.%Y”$>”; if ($mtdate==$today) { echo “Today”; } elseif ($mtdate==$yesterday) { echo “Yesterday”; } else { echo(”<$MTEntryDate format=”%A”$>”); } ?> i dnt know how to incorporate it into this script i have: //Display the row of data $output .= "<tr bgcolor=\"$bgcolor\">\n"; $output .= "<td>".$row['Receiver']."</td>\n"; $output .= "<td>".$row['Amount']."</td>\n"; $output .= "<td>".date('d/F/Y h:i:s a', strtotime($row['Date']))."</td>\n"; $output .= "</tr>\n"; if somebody cud help me out i would really would appreciate it. Thanks Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 24, 2008 Share Posted June 24, 2008 I don't recommend doing that. Getting a giant string with all of the content and echoing out the entire string as one variable (that's just me). As far as your problem. I am a little confused on what your trying to do? I think this is it based on what you said... Might not work but it'll give you something to play with. That's the generic idea. But I raelly don't like the idea of the code you found on the internet. It's really badly formatted and that MK line is actually going to throw a PHP syntax error. Never seen them try to format it that way before. It seems you want to get THAT day + the day before and show something different based on that. I havne't tested it but I worked up another set of code below that..it should do close to what you want but I don't have time to test it myself yet. It'll give you something to work with though that you can work off of and work out the bugs in. <?php //Display the row of data $output .= "<tr bgcolor=\"$bgcolor\">\n"; $output .= "<td>".$row['Receiver']."</td>\n"; $output .= "<td>".$row['Amount']."</td>\n"; $output .= "<td>"; $date = date('d/F/Y h:i:s a', strtotime($row['Date'])); $datebeforedate = date($date, strtotime("-1 day")); $mtdate = "<$MTEntryDate format="%m.%d.%Y"$>"; if ($mtdate == $today) { echo 'Today'; }else if { echo 'Yesterday'; }else { echo ("<$MTEntryDate format="%A"$>"); } $output .= "</td>\n"; $output .= "</tr>\n"; ?> Try This <?php //Display the row of data $output .= "<tr bgcolor=\"$bgcolor\">\n"; $output .= "<td>".$row['Receiver']."</td>\n"; $output .= "<td>".$row['Amount']."</td>\n"; $output .= "<td>"; $date_show = date('d/F/Y h:i:s a', strtotime($row['Date'])); $date_compare = date('d/F/Y h:i:s a', strtotime($row['Date'])); $date_today = date("m.d.Y"); $date_yesterday = date("m.d.Y", strtotime("-1 day")); if ($date_compare == $date_today) { echo 'Today'; }else if ($date_compare == $date_yesterday) { echo 'Yesterday'; }else { echo $date_show; } $output .= "</td>\n"; $output .= "</tr>\n"; ?> I am pretty sure the code under Try This will be what you are looking for. If it throws a syntax error or something let me know, I just don't feel like loading it up and checking it right now. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 24, 2008 Share Posted June 24, 2008 If you want to compare dates, they have to be in the same format m.d.Y date is not going to equal a d/F/Y date and certainly not if one has a time string and one doesn't. Quote Link to comment Share on other sites More sharing options...
RealDrift Posted June 24, 2008 Author Share Posted June 24, 2008 i tried the second script you gave me, it doesn't give me ay errors but it dsnt display the dates in the table anymore, it just echoes them in a line at the top of the page. and it dsnt substitute the date for the words "Today" and "Yesterday" also the date and time are stored int he database as a timestamp witht he standard format ??? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 25, 2008 Share Posted June 25, 2008 <?php $dhDateTime = '2008-06-23 21:30:25'; // from database list($date, $time) = explode (' ', $dhDateTime); if ($date == date('Y-m-d')) { echo 'Today '; } elseif ($date == date('Y-m-d', strtotime('-1 days'))) { echo 'Yesterday '; } else echo date ('d F Y ', strtotime($dhDateTime)); echo $time; ?> Quote Link to comment Share on other sites More sharing options...
RealDrift Posted June 25, 2008 Author Share Posted June 25, 2008 thanks barand for that but how do i incorporate it into the table script i have? Quote Link to comment Share on other sites More sharing options...
RealDrift Posted June 27, 2008 Author Share Posted June 27, 2008 could someone help me with this please? Quote Link to comment Share on other sites More sharing options...
siwelis Posted June 27, 2008 Share Posted June 27, 2008 It may help to know how you want it incorporated into your script... You can write it in pseudocode... eg; <?php //Display the row of data $output .= "<tr bgcolor=\"$bgcolor\">\n"; $output .= "<td>".$row['Receiver']."</td>\n"; $output .= "<td>".$row['Amount']."</td>\n"; $output .= "<td>".date('d/F/Y h:i:s a', strtotime($row['Date']))."</td>\n"; $output .= "<td>".$TodayOrYesterday."</td>\n"; //how do I find out what $TodayOrYesterday is? $output .= "</tr>\n"; ?> I'm just not really sure how you want it incorporated without an example... But, if that's the case, you can use the previously suggested code by the fellows before me, and just change the "ECHO"s to put it straight into the $output var... eg; <?php //Display the row of data $output .= "<tr bgcolor=\"$bgcolor\">\n"; $output .= "<td>".$row['Receiver']."</td>\n"; $output .= "<td>".$row['Amount']."</td>\n"; $date_show = date('d/F/Y h:i:s a', strtotime($row['Date'])); $date_compare = date('d/F/Y h:i:s a', strtotime($row['Date'])); $date_today = date("m.d.Y"); $date_yesterday = date("m.d.Y", strtotime("-1 day")); if ($date_compare == $date_today) { $output .= "<td>Today</td>\n"; // echo 'Today'; }else if ($date_compare == $date_yesterday) { $output .= "<td>Yesterday</td>\n"; // echo 'Yesterday'; }else { $output .= "<td>".$date_show."</td>\n"; }; $output .= "</tr>\n"; ?> Psst. I just figured out how to make the code color-coded... No pun intended... Include <?php AND ?> ! Quote Link to comment Share on other sites More sharing options...
RealDrift Posted June 28, 2008 Author Share Posted June 28, 2008 Thankyou Siwelis for incorporating the original code in the pseudo code, i am not too good with understanding how that kinda coding works. however i have a problem, the dates in the table show up but the words "Today" and "yesterday" are not shown. any ideas as to why? Quote Link to comment Share on other sites More sharing options...
RealDrift Posted June 28, 2008 Author Share Posted June 28, 2008 bump Quote Link to comment Share on other sites More sharing options...
RealDrift Posted June 29, 2008 Author Share Posted June 29, 2008 could someone help me out with this? Quote Link to comment Share on other sites More sharing options...
RealDrift Posted June 30, 2008 Author Share Posted June 30, 2008 please could someone assist me, i dont have any idea as to how i shud fix this. i thought an if elseif statement cud give me the results but it dsnt, anyone able to even make a wild guess? Quote Link to comment Share on other sites More sharing options...
RealDrift Posted June 30, 2008 Author Share Posted June 30, 2008 bump Quote Link to comment Share on other sites More sharing options...
Andy-H Posted July 8, 2008 Share Posted July 8, 2008 I am not very good at PHP but the obvious way it would seem to me is: $date = date('d/F/Y'); $time = date('h:i:s'); $row_date = $row['Date']; $day = explode("/", $date); $row_day = explode("/", $row_date); $time_disp = explode(" ", $row_date); if ($row_day[0] == $day[0]){ $echo = "Today - ".$time_disp[1]; }elseif ($row_day[0] == ($day[0] - 1) ){ $echo = "Yesterday - ".$time_disp[1]; }else{ $echo = $row_date; } THen add the display accordingly... Duno if it will work, as I said I'm not the best with PHP. Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted July 8, 2008 Share Posted July 8, 2008 <?php // Today's date $tday = date('d/F/Y'); // Yesterday's date $yday = date('d/F/Y', strtotime('-1 day')); // Date in question $rowdate = date('d/F/Y', strtotime($row['Date'])); // Display date if ($rowdate = $tday) { $ddate = "Today"; } elseif($rowdate = $yday) { $ddate = "Yesterday"; } else { $ddate = $rowdate; } //Display the row of data $output .= "<tr bgcolor=\"$bgcolor\">\n"; $output .= "<td>".$row['Receiver']."</td>\n"; $output .= "<td>".$row['Amount']."</td>\n"; $output .= "<td>".$ddate."</td>\n"; $output .= "</tr>\n"; // I'm not a fan of concatenating functions ... // MH ?> Any problems, email me matthew_haworth (at) hotmail.com I leave @ out to avoid junk mail ; ). Bare in mind that the date in $row['Date'] may not comply with the strtotime function.. Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 <?php // Today's date $tday = date('d/F/Y'); // Yesterday's date $yday = date('d/F/Y', strtotime('-1 day')); // Date in question $rowdate = date('d/F/Y', strtotime($row['Date'])); // Display date if ($rowdate == $tday) { $ddate = "Today"; } elseif($rowdate == $yday) { $ddate = "Yesterday"; } else { $ddate = $rowdate; } //Display the row of data $output .= "<tr bgcolor=\"$bgcolor\">\n"; $output .= "<td>".$row['Receiver']."</td>\n"; $output .= "<td>".$row['Amount']."</td>\n"; $output .= "<td>".$ddate."</td>\n"; $output .= "</tr>\n"; // I'm not a fan of concatenating functions ... // MH ?> Lol made an elementary mistake that i HAD to correct lol, jeez that was bad. Forgive the double post. Also bare in mind it requires the American date format for input M/D/Y from $rows['date'] TESTED: Works. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 9, 2008 Share Posted July 9, 2008 These comparisons won't work <?php if ($rowdate = $tday) { $ddate = "Today"; } elseif($rowdate = $yday) ?> Single "=" are used for assignment, double "==" are used for comparison: <?php if ($rowdate == $tday) { $ddate = "Today"; } elseif($rowdate == $yday) ?> Ken Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted July 9, 2008 Share Posted July 9, 2008 These comparisons won't work <?php if ($rowdate = $tday) { $ddate = "Today"; } elseif($rowdate = $yday) ?> Single "=" are used for assignment, double "==" are used for comparison: <?php if ($rowdate == $tday) { $ddate = "Today"; } elseif($rowdate == $yday) ?> Ken I had already noticed that, thank you however Ken. The solution that I posted is definitely functional given that $row['Date'] produces the create format for strtotime() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.