Solved

PHP count back from "NOW" to a date in this format "2015-01-22 17:57:51"

Posted on 2015-02-05
3
70 Views
Last Modified: 2015-02-05
Hi

I have a date in the following format 2015-01-22 17:57:51 and I want to be able to count this down from "now" and present something like this:

if it's only minutes:
something like:   "23 minutes ago"

if it's hours and minutes (but less than a day):
something like:  "1 hour 23 mins ago"

if it's days, hours and minutes
something like:  "2 days 1 hour 23 mins ago"
0
Comment
Question by:NeilT
3 Comments
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
Comment Utility
<?php 
function get_timespan_string($older, $newer) { 
  $Y1 = $older->format('Y'); 
  $Y2 = $newer->format('Y'); 
  $Y = $Y2 - $Y1; 

  $m1 = $older->format('m'); 
  $m2 = $newer->format('m'); 
  $m = $m2 - $m1; 

  $d1 = $older->format('d'); 
  $d2 = $newer->format('d'); 
  $d = $d2 - $d1; 

  $H1 = $older->format('H'); 
  $H2 = $newer->format('H'); 
  $H = $H2 - $H1; 

  $i1 = $older->format('i'); 
  $i2 = $newer->format('i'); 
  $i = $i2 - $i1; 

  $s1 = $older->format('s'); 
  $s2 = $newer->format('s'); 
  $s = $s2 - $s1; 

  if($s < 0) { 
    $i = $i -1; 
    $s = $s + 60; 
  } 
  if($i < 0) { 
    $H = $H - 1; 
    $i = $i + 60; 
  } 
  if($H < 0) { 
    $d = $d - 1; 
    $H = $H + 24; 
  } 
  if($d < 0) { 
    $m = $m - 1; 
    $d = $d + get_days_for_previous_month($m2, $Y2); 
  } 
  if($m < 0) { 
    $Y = $Y - 1; 
    $m = $m + 12; 
  } 
  $timespan_string = create_timespan_string($Y, $m, $d, $H, $i, $s); 
  return $timespan_string; 
} 

function get_days_for_previous_month($current_month, $current_year) { 
  $previous_month = $current_month - 1; 
  if($current_month == 1) { 
    $current_year = $current_year - 1; //going from January to previous December 
    $previous_month = 12; 
  } 
  if($previous_month == 11 || $previous_month == 9 || $previous_month == 6 || $previous_month == 4) { 
    return 30; 
  } 
  else if($previous_month == 2) { 
    if(($current_year % 4) == 0) { //remainder 0 for leap years 
      return 29; 
    } 
    else { 
      return 28; 
    } 
  } 
  else { 
    return 31; 
  } 
} 

function create_timespan_string($Y, $m, $d, $H, $i, $s) 
{ 
  $timespan_string = ''; 
  $found_first_diff = false; 
  if($Y >= 1) { 
    $found_first_diff = true; 
    $timespan_string .= pluralize($Y, 'year').' '; 
  } 
  if($m >= 1 || $found_first_diff) { 
    $found_first_diff = true; 
    $timespan_string .= pluralize($m, 'month').' '; 
  } 
  if($d >= 1 || $found_first_diff) { 
    $found_first_diff = true; 
    $timespan_string .= pluralize($d, 'day').' '; 
  } 
  if($H >= 1 || $found_first_diff) { 
    $found_first_diff = true; 
    $timespan_string .= pluralize($H, 'hour').' '; 
  } 
  if($i >= 1 || $found_first_diff) { 
    $found_first_diff = true; 
    $timespan_string .= pluralize($i, 'minute').' '; 
  } 
  if($found_first_diff) { 
    $timespan_string .= 'and '; 
  } 
  $timespan_string .= pluralize($s, 'second'); 
  return $timespan_string; 
} 

function pluralize( $count, $text ) 
{ 
  return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) ); 
} 

date_default_timezone_set('Europe/Amsterdam');

$date1 = new DateTime("2015-01-22 17:57:51");
$date2 = new DateTime("now");

echo get_timespan_string($date1,$date2);


?>

Open in new window

0
 
LVL 3

Author Closing Comment

by:NeilT
Comment Utility
Wow, many thanks Kimputer :)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
For future reference, you may want to search the E-E articles before you post a question.  You can find this and much more about how to handle datetime values in PHP here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Sample, implementing the Practical Application #3 in the Article:
http://iconoun.com/demo/temp_neilt.php
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now