[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

date() comparison in php

Posted on 2009-02-13
3
Medium Priority
?
3,740 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:Catcherman16
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
striker46 earned 2000 total points
ID: 23638275
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;
	
?>

Open in new window

0
 
LVL 5

Expert Comment

by:striker46
ID: 23638462
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)); 
 
}  
 
 
?>

Open in new window

0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 23638476
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';
}
 
?>

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question