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) {
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'));
I spent some time doing this quick little function (I didn't use the default one because I wanted a bit more customization later on). I made a post that has $checkTime = '0';, and when run through this function it comes back as 49 years ago.
Why is it returning that when January 1970 was only 45 years ago? Are the extra 4 years coming from time differences and leap years?
The other times seem to work correct (recent ones), but the ones I set to 0 say that and I'm just curious where the bug is, or what I might be overlooking.
function relativeTime($string) {
$currentTime = time();
$checkTime = $string;
$timeDifference = $currentTime - $checkTime;
if($timeDifference > '0') {
$timeSeconds = round(($timeDifference / 60) * 60);
$timeMinutes = round($timeSeconds / 60);
$timeHours = round($timeMinutes / 60);
$timeDays = round($timeHours / 24);
$timeWeeks = round($timeDays / 7);
$timeMonths = round($timeWeeks / 4);
$timeYears = round($timeMonths / 12);
if($timeSeconds < '2') {
return ''.$timeSeconds.' second ago';
} elseif($timeSeconds < '60') {
return ''.$timeSeconds.' seconds ago';
} elseif($timeMinutes < '2') {
return ''.$timeMinutes.' minute ago';
} elseif($timeMinutes < '60') {
return ''.$timeMinutes.' minutes ago';
} elseif($timeHours < '2') {
return ''.$timeHours.' hour ago';
} elseif($timeHours < '24') {
return ''.$timeHours.' hours ago';
} elseif($timeDays < '2') {
return ''.$timeDays.' day ago';
} elseif($timeDays < '7') {
return ''.$timeDays.' days ago';
} elseif($timeWeeks < '2') {
return ''.$timeWeeks.' week ago';
} elseif($timeWeeks < '4') {
return ''.$timeWeeks.' weeks ago';
} elseif($timeMonths < '2') {
return ''.$timeMonths.' month ago';
} elseif($timeMonths < '12') {
return ''.$timeMonths.' months ago';
} elseif($timeYears < '2') {
return ''.$timeYears.' year ago';
} elseif($timeYears > '1') {
return ''.$timeYears.' years ago';
} else {
return $timeSeconds;
}
} else {
return 'The Future';
}
}
Because your calculations are messed up. See one example and check all your formulas
<?
//same numbers, different formula
$checkTime=0;
echo (time()-$checkTime)/31536000; //45.094949422882 Years
?>
31536000 is the number of seconds in 1 year.
Even using that you will have to take care about leap years. We cant divide a timestamp by minutes then by hours then by days and so on. If you need accurate results the input has to be accurate as well.
Remember the famous Bi-Weekly and Twice Monthly payouts used commonly in USA? From a distance they both appear to mean the same thing but they don't. So dividing like your code is doing looses all that accuracy and when that difference is multiplied to 45 years it becomes substantial.
Fiddle
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.
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";
}
}
Convert 2010-04-16 16:30:00 to "Tomorrow Afternoon" or convert another date to "this afternoon", "next year", "next week wednesday". You get the picture.
Anyone know of a PHP or Javascript library that can do this?
I think you can come a long way with what is said here: Calculate relative time in C#
The logic is there, and it's not too hard to do the javascript equivalent if a solution in a different language suits you.
There might be more elegant solutions out there (look for Natural language formatting), but personally I couldn't find any.
I would suggest calculating the distance from now to the date you're formatting, and using thresholds.
Pseudo solution:
diff = now - date
if (diff < one_day)
format for today
if (diff < two_days)
format for tomorrow
if (diff < one_week)
format using days from now
.
.
.
The comparison will work for both past and future dates, as long as you use compare with the abs value of diff. Display timeunit ago or timeunit from now by checking if diff is positive or negative.
For the morning, afternoon, evening etc. you only need to check for the time of day in the date, and regarding the formatting type you hit, either display the time as numbers (far away), or natural language (recent or near date).
function gett($sam){
$times = time() - $sam;
if ($times == 60){
$times = "a minute ago";
}
if (($times != 1) && ($times < 60) && ($times != 0)){
$times = "$times seconds ago";
}
if ($times == 0){
$times = "less than a second ago";
}
if ($times == 1){
$times = "a second ago";
}
if ($times > 60 && $times < 3600){
$times = ceil($times/60)." minutes ago";
}
if($times == 3600){
$times = "an hour ago";
}
if($times > 3600 && $times < 86400){
$times = ceil($times/3600)." hours ago";
}
if($times == 86400){
$times = "a day ago";
}
if($times > 86400){
$times = ceil($times/86400)." days ago";
}
return $times; }
Usage:
$updated = gett($timestamp);
where $timestamp is pretty self-explanatory..
From this link -> How do I calculate relative time in C#?
function posted(t) {
var now = new Date();
var diff = parseInt((now.getTime() - Date.parse(t)) / 1000);
if (diff < 60) { return 'less than a minute ago'; }
else if (diff < 120) { return 'about a minute ago'; }
else if (diff < (2700)) { return (parseInt(diff / 60)).toString() + ' minutes ago'; }
else if (diff < (5400)) { return 'about an hour ago'; }
else if (diff < (86400)) { return 'about ' + (parseInt(diff / 3600)).toString() + ' hours ago'; }
else if (diff < (172800)) { return '1 day ago'; }
else {return (parseInt(diff / 86400)).toString() + ' days ago'; }
}