1
$time_frame = floor(abs((strtotime($notification['note_date'])-strtotime(date("Y-m-d H:i:s")))/60/60));       

if($time_frame>24){
    $time_frame = floor($time_frame/24);
    if($time_frame>1){
        $time_frame = $time_frame." days ago";
    } else{
        $time_frame = $time_frame." day ago";
    }
} else if($time_frame>1) {
    $time_frame = $time_frame." hours ago";
} else if($time_frame==1) {
    $time_frame = $time_frame." hour ago";
} else{
    $time_frame = "1 hour ago"; //I want to break this hour in to minutes
} 

How do i break that hour in to display in to minutes, last else statement.

Qirel
  • 25,449
  • 7
  • 45
  • 62
Kiran Bhattarai
  • 175
  • 1
  • 12
  • 1
    Think you might want to have a look at this question, https://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago – Qirel Oct 23 '18 at 15:21
  • @Qirel I used the solution from https://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago. so i am using ` $time_frame = time_elapsed_string($notification['note_date']);` All the note_date are stored in the database. suppose i want to separate the notification that contains **second,seconds,minute,minutes,hour,hours** with a new title and the rest with the new title. I am calling function `time_elapsed_string($notification['note_date']);` inside my `foreach`. $time_frame is only initialized inside the loop. – Kiran Bhattarai Oct 23 '18 at 16:34

3 Answers3

2

I recommend dealing with seconds/timestamps or date-time objects instead - this would be a better and more dynamic approach in my opinion. Perhaps have a look at this question instead Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...

That said, if you want to do it with your current approach, you can multiply your variable by 60 (as there is 60 minutes in an hour), such as

$time_frame  = 60*$timeframe;

Examples,
If $timeframe is 1, then you have exactly one hour. 60min * 1 = 60 minutes.
If $timeframe is 0.5, that would be half an hour. 60min * 0.5 = 30 minutes.
If $timeframe is 0.25, means that 15 minutes have passed, and 60min * 0.25 = 15 minutes.

You might want to round that number to your liking, so that you will not get output such as 1.43 minutes left. Also note that floating point numbers may not be exactly accurate, hence my recommendation of using datetime objects or timestamps instead.

Qirel
  • 25,449
  • 7
  • 45
  • 62
2

If you use the DateTime class, you can use the diff() method so you don't have to mess with all the calculating. diff() returns a DateInterval which has public properties you can use to determine the appropriate message.

$interval = date_create($notification['note_date'])->diff(new DateTime);

if ($interval->days > 1) {
    $time_frame = "{$interval->days} days";
} elseif ($interval->days == 1) {
    $time_frame = "1 day";
} elseif ($interval->h > 1) {
    $time_frame = "{$interval->h} hours";
} elseif ($interval->h == 1) {
    $time_frame = "1 hour";
} elseif ($interval->i > 1) {
    $time_frame = "{$interval->i} minutes";
} elseif ($interval->i == 1) {
    $time_frame = "1 minute";
} else {
    $time_frame = "Less than 1 minute";
}

echo "{$time_frame} ago";
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

You can actually USE that string! :-D

$x = new DateTime('1 hour ago'); // done at 17:17
echo $x->format('Y-m-d H:i:s');  // outputs 2018-10-23 16:17:12

https://3v4l.org/jWTC8

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • 1
    That's not what OP is asking, though. :) I believe he is asking how one can display the number of minutes ago, instead of "1 hour ago". – Qirel Oct 23 '18 at 15:19
  • Well, that's pretty obvious. One hour ago is 60 minutes. – delboy1978uk Oct 23 '18 at 15:20
  • 1
    His current code displays "1 hour ago" when its one hour or less ago. He's asking how one can display "X minutes ago" instead of "1 hour ago" for all entries below one hour. – Qirel Oct 23 '18 at 15:21
  • ah ok got you now – delboy1978uk Oct 23 '18 at 15:21
  • i will get some decimal value on the $time_frame, for eg if i have 0.5 on the $time_frame how will i calulate it in to minutes. it should equals to 30 min. i dont want to create a more loops. i am looking for a short answer . – Kiran Bhattarai Oct 23 '18 at 15:24