0

how can i categorize database rows by

today

yesterday

last 7 days

last 30 days

Webber Depor
  • 198
  • 4
  • 16
  • 1
    What did you try? What is your data structure? – msanford Dec 04 '15 at 19:31
  • i will just get Datetime. I dont now how to use it after getting it. No need for DB structure. – Webber Depor Dec 04 '15 at 19:33
  • Possible duplicate of [How to check if a date is in a given range?](http://stackoverflow.com/questions/976669/how-to-check-if-a-date-is-in-a-given-range) –  Dec 05 '15 at 07:29
  • if you already have a Datetime object, you may want to take a look at the third highest answer http://stackoverflow.com/a/9065661/5051310 –  Dec 05 '15 at 07:30
  • Don't forget to mark as an Accepted Answer with the green check mark below the up and down arrows. If it solved your problem. That's how we roll on the stack. – Drew Mar 03 '16 at 02:49

2 Answers2

1

Here is a function for checking post time:

$post_time1 = strtotime("05 December 2015");
$post_time2 = strtotime("02 December 2015");

check_post_time($post_time1);
check_post_time($post_time2);

function check_post_time($time) {
    $before_24 = strtotime("-1 day");
    $before_1week = strtotime("-1 week");
    $before_1month = strtotime("-1 month");
    if($time >= $before_24)
        echo "24hr ago";
    elseif($time >= $before_1week)
        echo "between 24hr - 1 week ago";
    elseif($time >= $before_1month)
        echo "between 7 days - 1 month ago";
    else
        echo "1 month ago";
}
im_tsm
  • 1,965
  • 17
  • 29
1

You can use Carbon PHP package to display your time in human readable way. This package is very popular and it even included on the Laravel framework.

You can install the package using Composer. Once you installed this package you can use it by following like this:

<?php
require 'vendor/autoload.php';

use Carbon\Carbon;

printf("Now: %s", Carbon::now());

// Human readable way
echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
Md Mazedul Islam Khan
  • 5,318
  • 4
  • 40
  • 66