6

HI, i have a couple of posts in my MySql database server, one of the info content in each post is the date and time in the format datetime (Ex. 2010-11-26 21:55:09) when the post was made.

So, i want to retrive the actual date and time from the SQL server with the function NOW() and calculates how many seconds or minutes or hours or days ago was post the info.

I dont know how to create this php script but i know that for sure is allready made, so thanks for any help.

DomingoSL
  • 14,920
  • 24
  • 99
  • 173

5 Answers5

5

you could use the date_diff() function

http://php.net/manual/en/function.date-diff.php

Something like...

<?php 
$now = time();
$then = $posttime;
$diff = date_diff($now,$then);
echo $diff->format('%R%d days'); #change format for different timescales
?>

edit --

I actually solve this issue on one of my twitter apps using this function...

function time_since ( $start )
{
    $end = time();
    $diff = $end - $start;
    $days = floor ( $diff/86400 ); //calculate the days
    $diff = $diff - ($days*86400); // subtract the days
    $hours = floor ( $diff/3600 ); // calculate the hours
    $diff = $diff - ($hours*3600); // subtract the hours
    $mins = floor ( $diff/60 ); // calculate the minutes
    $diff = $diff - ($mins*60); // subtract the mins
    $secs = $diff; // what's left is the seconds;
    if ($secs!=0) 
    {
        $secs .= " seconds";
        if ($secs=="1 seconds") $secs = "1 second"; 
    }
    else $secs = '';
    if ($mins!=0) 
    {
        $mins .= " mins ";
        if ($mins=="1 mins ") $mins = "1 min "; 
        $secs = '';
    }
    else $mins = '';
    if ($hours!=0) 
    {
        $hours .= " hours ";
        if ($hours=="1 hours ") $hours = "1 hour ";             
        $secs = '';
    }
    else $hours = '';
    if ($days!=0) 
    {
        $days .= " days "; 
        if ($days=="1 days ") $days = "1 day ";                 
        $mins = '';
        $secs = '';
        if ($days == "-1 days ") {
            $days = $hours = $mins = '';
            $secs = "less than 10 seconds";
        }
    }
    else $days = '';
    return "$days $hours $mins $secs ago";
}

You pass it in a unix timestamp of the time to check (the post time) and it returns the various string.

billythekid
  • 441
  • 4
  • 16
  • 2
    You sir, just made my day. I've been needing something like this for a while, now I can actually count the number of minutes between two timestamps, completely in PHP. I've made it a GIST if anyone is interested, or would like to make it more efficient: https://gist.github.com/4347159 – ZettaGeek Dec 20 '12 at 17:49
  • I don't think date_diff takes time() as an input. It takes DateTime objects. So you would need to do something like: `$date1=date_create("2013-03-15");` `$date2=date_create("2013-12-12");` `$diff=date_diff($date1,$date2);` – Jimmy Pelton Dec 19 '16 at 16:11
4

Usually, you do this kind of thing in a query, but MySQL isn't very good with intervals (it would be very easy with PostgreSQL). You could convert it to unix timestamp, then it would give the number of seconds between the two dates :

SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(your_datetime_column);

I thought about DATEDIFF, but it only returns the number of days between the two dates.

You can do it in PHP, for instance, with DateTime class :

$date1 = new DateTime();
$date2 = new Datetime('2010-11-26 12:00:00');

var_dump($date1->diff($date2));

(There's a procedural way to do this, if you're not a fan of OOP.)
This is definitely the solution I'd use if I can't do it with the RDBMS. DateTime::diff returns a DateInterval object, which contains the number of seconds, minutes, hours, days, etc. between the two dates.

You could also do it with timestamps in PHP :

$num_sec = time() - strtotime('2010-11-26 12:00:00');

Which would return the same thing as the SQL query.

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
  • The one thing to watch out for is DateTime->diff is a 5.3+ feature, so if you're on 5.2.x, it won't work for you and you'll have to go with either the SQL or convert the time into unix timestamps in PHP and do it there. – MBCook Nov 27 '10 at 03:40
4

As billythekid said, you can use the date_diff() function if you are using PHP5.3+, if you are not then there are various methods. As shown by other posters. The quickest method in MySQL if you want to know the time split in to the "hours:mins:secs" hierarchy is to use the TIMEDIFF() function.

SELECT TIMEDIFF(NOW(), '2010-11-26 12:00:00');

If you want it as seconds, use the unix timestamp features in MySQL or in PHP, you can convert MySQL dates to PHP quickly using strtotime().

Orbling
  • 20,413
  • 3
  • 53
  • 64
0

An easy solution is possible from within the SQL Query:

SELECT UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(post_date) AS seconds_ago FROM posts

Documentation here: MySQL Ref

Nick
  • 1,799
  • 13
  • 13
0

I actually needed to do this in PHP myself and while billythekid's post was in the right direction it fell short. I've minimized the code though it should be clear that the second parameter is from a database with a DATETIME column type.

<?php
$interval = date_diff(date_create(date('Y-m-d H:i:s')), date_create($row1['date']));
echo $interval->format('%R%a days');

//Database: 2019-02-22
//PHP's date: 2018-07-07
//Result: +306 days
?>

A reminder of the obvious: you can also just use substr($interval->format('%R%a days'),1) if you need just the integer.

John
  • 1
  • 13
  • 98
  • 177