1

I want to convert date() to "" years/months/weeks/days/hours/minutes/seconds ago. How do I do that?

I'm currently using date('M j, Y') when saving the date into a database. Is there a more effective way to save the date for what I want to produce above?

(My site is not live yet so I can do whatever I need in the database and code if need be)

Anthony
  • 471
  • 1
  • 5
  • 12

1 Answers1

3

You should always save your dates in MySQL's datetime format (YYYY-MM-DD). This allows you to easily take advantage of MySQL's built in date functionality. Storing it in any other format means (potentially a lot) more work for you when you want to do more then just display those values.

To accomplish what you want to do with PHP you would DateTime() (based on this answer):

$datetime1 = new DateTime($firstDate);
$datetime2 = new DateTime($secondDate);
$interval  = $datetime1->diff($datetime2);
if ($interval->days <= 7)
{
    $elapsed   = $interval->format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds');
    $elapsed   = str_replace(array('0 years,', ' 0 months,', ' 0 days,',  ' 0 hours,', ' 0 minutes,'), '', $elapsed);
    $elapsed   = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ',  ' 1 hours, ', ' 1 minutes'), array('1 year, ', '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), $elapsed);
    echo $elapsed;
}
else 
{
    echo $firstDate;
}

$datetime1 = new DateTime($firstDate);
$datetime2 = new DateTime($secondDate);

These lines create DateTime() objects with their respective dates.

$interval  = $datetime1->diff($datetime2);

This lines subtracts the second date from the first and returns the difference as a DateInterval() object.

if ($interval->days > 7)

This line checks to see seven or more days have elapsed between the two dates. If so the first code block is executed. If not, the first date is printed out.

$elapsed   = $interval->format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds');
$elapsed   = str_replace(array('0 years,', ' 0 months,', ' 0 days,',  ' 0 hours,', ' 0 minutes,'), '', $elapsed);
$elapsed   = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ',  ' 1 hours, ', ' 1 minutes'), array('1 year, ', '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), $elapsed);
echo $elapsed;

This code block just takes the date difference between the two dates (a DateInterval() object) and formats it in the format you requested. The second lines removes any periods of time that have no values (i.e. 0 months) and removes them from the string. The third line takes any periods with one value (i.e. 1 months) and trims off the unnecessary 's' at the end (i.e. 1 months becomes 1 month).

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Thanks. Can you quickly explain the code? I'm new to php it would help me alot. Thanks! – Anthony Sep 18 '13 at 00:44
  • @Anthony Explanation added – John Conde Sep 18 '13 at 00:52
  • Thanks a lot. However my format was actually supposed to be like Ex. 35 seconds ago. OR 15 minutes ago. OR 2 days ago. OR 1 month ago OR 1 year ago. Your code gave year, days and seconds all together. It has a purpose I suppose but that's not quite what I was looking for. – Anthony Sep 18 '13 at 00:59
  • `$interval->a` ? Property `a` doesn't exists in [DateInterval](http://php.net/manual/en/class.dateinterval.php). You probably meant `$interval->days` ? – Glavić Sep 18 '13 at 01:30
  • @Glavić Good catch. Although I think it is `$interval->d`. – John Conde Sep 18 '13 at 01:32
  • No, it is not `d` parameter, it is `days`; because `days` parameter hold sum of all days between 2 intervals. Example: difference between `2013-01-01` and `2013-02-01` is `1m 0d` (`$interval->m . $interval->d`) or `31days` (`$interval->days`). – Glavić Sep 18 '13 at 01:37
  • You're absolutely right. It's late here. I think I'm done braining for the day. :) – John Conde Sep 18 '13 at 02:01
  • Now is +1 ;) Same here, going off ;) – Glavić Sep 18 '13 at 02:04