This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 9 years ago.
I have a code
echo date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td></tr>';
The result:
23-10-2013 16:28
Is there a way to change into like:
Posted 12 minutes ago
or / &
Just Posted
Whats the easiest way of doing this?
strtotime will help you to convert the date and make operations over it.
function elapsedTimeAgo ($newTime) {
$timeCalc = time() – strtotime($newTime);
$elapsedTimeText = "";
if ($timeCalc > (60*60*24)) {
$elapsedTimeText = round($timeCalc/60/60/24) . "days ago";
} else if ($timeCalc > (60*60)) {
$elapsedTimeText = round($timeCalc/60/60) . "hours ago";
} else if ($timeCalc > 60) {
$elapsedTimeText = round($timeCalc/60) . "minutes ago";
} else if ($timeCalc > 0) {
$elapsedTimeText .= "seconds ago";
} else {
$elapsedTimeText .= "Just Posted";
}
return $elapsedTimeText;
}
echo elapsedTimeAgo($posts_row['post_date']);
To fetch the difference of seconds between current time and posted time:
$number_of_seconds = strtotime("now") - strtotime($posts_row['post_date']);
Then, you can apply your logic to display how many seconds ago, just now, etc.
Related
This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 3 years ago.
I want to compare two times which is in 12hrs format, but before comparing I have converted into strtotime and passed converted
values to function and comparing it but output return 0 even there is time difference of 60 minutes still getting 0 in output.
<?php
function timediff($start, $end)
{
if($end >= $start) {
return (round(($end-$start)/3600, 0))." Hrs ".((($end-$start)%3600)/60)." Min"; // time should be in queue
} else {
return (round(($start-$end)/3600, 0))." Hrs ".((($start-$end)%3600)/60)." Min";
}
}
echo timediff(strtotime('09: am'), strtotime('08: am'));
you should pass a valid time string, try it
function timediff($start, $end) {
if ($end >= $start) {
return (round(($end - $start) / 3600, 0)) . " Hrs " . ((($end - $start) % 3600) / 60) . " Min";
} else {
return "-" . (round(($start - $end) / 3600, 0)) . " Hrs " . ((($start - $end) % 3600) / 60) . " Min";
}
}
echo timediff(strtotime('09:00 am'), strtotime('08:00 am'));
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
How to convert hh:mm:ss to minutes
(7 answers)
Closed 4 years ago.
$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.
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.
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";
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
I have a time left function that I am using to get the time left based on a sent parameter. My issue is I am having difficulty calculating if there is a day, a year, or a month left.
Function:
function get_time_difference_php_left($created_time)
{
$str = strtotime($created_time);
$today = strtotime(date('Y-m-d H:i:s'));
// It returns the time difference in Seconds...
$time_differnce = $today-$str;
// To Calculate the time difference in Years...
$years = 60*60*24*365;
// To Calculate the time difference in Months...
$months = 60*60*24*30;
// To Calculate the time difference in Days...
$days = 60*60*24;
// To Calculate the time difference in Hours...
$hours = 60*60;
// To Calculate the time difference in Minutes...
$minutes = 60;
if(intval($time_differnce/$years) > 1)
{
return " - ". intval($time_differnce/$years)." years left";
}else if(intval($time_differnce/$years) > 0)
{
return " - ".intval($time_differnce/$years)." year left";
}else if(intval($time_differnce/$months) > 1)
{
return " - ".intval($time_differnce/$months)." months left";
}else if(intval(($time_differnce/$months)) > 0)
{
return " - ".intval(($time_differnce/$months))." month left";
}else if(intval(($time_differnce/$days)) > 1)
{
return " - ".intval(($time_differnce/$days))." days left";
}else if (intval(($time_differnce/$days)) > 0)
{
return " - ".intval(($time_differnce/$days))." day left";
}else if (intval(($time_differnce/$hours)) > 1)
{
return " - ".intval(($time_differnce/$hours))." hours left";
}else if (intval(($time_differnce/$hours)) > 0)
{
return " - ".intval(($time_differnce/$hours))." hour left";
}else if (intval(($time_differnce/$minutes)) > 1)
{
return " - ".intval(($time_differnce/$minutes))." minutes left";
}else if (intval(($time_differnce/$minutes)) > 0)
{
return " - ".intval(($time_differnce/$minutes))." minute left";
}else if (intval(($time_differnce)) > 1)
{
return " - ".intval(($time_differnce))." seconds left";
}else
{
return " - few seconds left";
}
}
If I run this based on the now time and a date time of: 2014-04-17 03:27:26 it will tell me 88 years.
Suggestions, thoughts?
You had March initially. You are doing the calculation backwards then. Your code should show expiration date, not creation date. If you are trying to use an expiration date, then your code should use:
$time_differnce = $str - $today;
You can use the modulus (remainder) operator in each if to show the next value as well. For example if you have 3.7 days, you use 3 days and then .7*$hours.
Change all else if statements to if's
Change from:
if(intval($time_differnce/$years) > 1) {
return " - ". intval($time_differnce/$years)." years left";
} else if (intval($time_differnce/$years) > 0) {
to
if(intval($time_differnce/$years) > 1) {
return " - ". intval($time_differnce/$years)." years left";
}
if (intval($time_differnce/$years) > 0) {
This question already has answers here:
Convert number of minutes into hours & minutes using PHP
(14 answers)
Closed 9 years ago.
I wonder if there is a function in php or codeigniter which can do this :
function transformTime($min)
{
$min=(int)$min;
$heure=(int)($min/60);
$minute=(($min/60)-$heure)*60;
return $heure .':' . $minute . ':00';
}
I want convert x minutes to a time format.
Do something like this
<?php
function transformTime($min)
{
$ctime = DateTime::createFromFormat('i', $min);
$ntime= $ctime->format('H:i:s');
return $ntime;
}
echo transformTime(60); // "Prints" 01:00:00
You are almost there, just format the output with sprintf() or str_pad():
function transformTime($min)
{
$hour = floor($min / 60);
$min -= $hour * 60;
return sprintf('%02d:%02d:00', $hour, $min);
}
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 9 years ago.
I need to find time difference for like in facebook messages using php.
like: 2 weeks ago, 2 hr 30 mins ago, one second ago
My time format is "Y-m-d H:i:s"
Can Anyone help me with it?
Store the message created time in database then use below function :
function get_time_difference_php($created_time)
{
date_default_timezone_set('Asia/Calcutta'); //Change as per your default time
$str = strtotime($created_time);
$today = strtotime(date('Y-m-d H:i:s'));
// It returns the time difference in Seconds...
$time_differnce = $today-$str;
// To Calculate the time difference in Years...
$years = 60*60*24*365;
// To Calculate the time difference in Months...
$months = 60*60*24*30;
// To Calculate the time difference in Days...
$days = 60*60*24;
// To Calculate the time difference in Hours...
$hours = 60*60;
// To Calculate the time difference in Minutes...
$minutes = 60;
if(intval($time_differnce/$years) > 1)
{
return intval($time_differnce/$years)." years ago";
}else if(intval($time_differnce/$years) > 0)
{
return intval($time_differnce/$years)." year ago";
}else if(intval($time_differnce/$months) > 1)
{
return intval($time_differnce/$months)." months ago";
}else if(intval(($time_differnce/$months)) > 0)
{
return intval(($time_differnce/$months))." month ago";
}else if(intval(($time_differnce/$days)) > 1)
{
return intval(($time_differnce/$days))." days ago";
}else if (intval(($time_differnce/$days)) > 0)
{
return intval(($time_differnce/$days))." day ago";
}else if (intval(($time_differnce/$hours)) > 1)
{
return intval(($time_differnce/$hours))." hours ago";
}else if (intval(($time_differnce/$hours)) > 0)
{
return intval(($time_differnce/$hours))." hour ago";
}else if (intval(($time_differnce/$minutes)) > 1)
{
return intval(($time_differnce/$minutes))." minutes ago";
}else if (intval(($time_differnce/$minutes)) > 0)
{
return intval(($time_differnce/$minutes))." minute ago";
}else if (intval(($time_differnce)) > 1)
{
return intval(($time_differnce))." seconds ago";
}else
{
return "few seconds ago";
}
}