We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# date() comparison in php

on
Medium Priority
3,752 Views
What i am trying to do, is grab a date from a MySQL table, then compare it to the current days date.

say i get the store date and it is "2009-2-12" and i compare it to \$todayDate which is 2009-2-13.

I want output something like:

"Last update: Yesterday"

or say today was the 12th, then it would say:

"Last update: Today"

and if it is more than a week sense the last update, i want it to say:

"Last update: Over a Week Ago"

how would i go about doing this?
Comment
Watch Question

## View Solution Only

Commented:
I attach a sample code which will return the time lapsed since a past moment. Basically it compares both UNIX times (now and past).

Then it returns 1 or 2 elements. For instance:

2 days, 3 hours
1 year, 2 months
1 hour, 45 minutes

\$original is the time of the past. In the example I wrote a UNIX timestamp of few days ago.

Hope it helps!
<?

\$original = 1234310400;

// array of time period chunks
\$chunks = array(
array(60 * 60 * 24 * 365 , year , years),
array(60 * 60 * 24 * 30 , month , months),
array(60 * 60 * 24 * 7, week , weeks),
array(60 * 60 * 24 , day , days),
array(60 * 60 , hour , hours),
array(60 , minute , minutes),
);

\$today = time(); /* Current unix time  */
\$since = \$today - \$original;

// \$j saves performing the count function each time around the loop
for (\$i = 0, \$j = count(\$chunks); \$i < \$j; \$i++) {

\$seconds = \$chunks[\$i][0];
\$name = \$chunks[\$i][1];
\$names = \$chunks[\$i][2];

// finding the biggest chunk (if the chunk fits, break)
if ((\$count = floor(\$since / \$seconds)) != 0) {
break;
}
}

\$print = (\$count == 1) ? '1 '.\$name : "\$count {\$names}";

if (\$i + 1 < \$j) {
// now getting the second item
\$seconds2 = \$chunks[\$i + 1][0];
\$name2 = \$chunks[\$i + 1][1];
\$names2 = \$chunks[\$i + 1][2];

// add second item if it's greater than 0
if ((\$count2 = floor((\$since - (\$seconds * \$count)) / \$seconds2)) != 0) {
\$print .= (\$count2 == 1) ? ', 1 '.\$name2 : ", \$count2 {\$names2}";
}
}

echo \$print;

?>

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
This code does exactly what you're asking for.
<?

// This is just for the example

\$samplepasttime = "1234310400";

// Select the times

\$past = \$samplepasttime;
\$now = time();

\$date_diff = subtract_dates(\$past, \$now);

if (\$date_diff <= 7){
switch (\$date_diff) {
case 0:
\$when = "Today";
break;
case 1:
\$when = "Yesterday";
break;
case 2:
\$when = "2 Days ago";
break;
case 3:
\$when = "3 Days ago";
break;
case 4:
\$when = "4 Days ago";
break;
case 5:
\$when = "5 Days ago";
break;
case 6:
\$when = "6 Days ago";
break;
case 7:
\$when = "One Week ago";
break;
}
} elseif(\$date_diff > 7){

\$when = "Over a Week Ago";

}

echo "Last updated: " . \$when;

// This subtracts the dates

function subtract_dates(\$begin_date, \$end_date)

{

return round(((\$end_date - \$begin_date) / 86400));

}

?>

Commented:
Simply use the strtotime http://us.php.net/strtotime function, it is very versatile, e.g. you can pass to it your date or even text values such as "-1 day" or "-1 week":

<?php

// If the date is older than 1 week
if(strtotime("2009-2-12") < strtotime("-1 week")) {
echo 'Last update: Over a Week Ago';
}

?>
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile