Jump to content

Today and Yesterday Date function?


RealDrift

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

???

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ?> !

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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..

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.