How to show month ago in PHP - php

$logintime value 1 year finished means, it will showing 1 years ago, but suppose 2 months only finished means I want to show 2 months ago, but my code showing like 60 days ago, I don't know where I did mistake, remaining hour, minutes this are working fine, only month making problem, $logintime = 2016-02-27 03:00:00
function timeAgo($logintime) {
date_default_timezone_set('UTC');
date_default_timezone_set('Asia/Kolkata');
$start_date = new DateTime($logintime);
$since_start = $start_date->diff(new DateTime(date("Y-m-d h:i:s")));
if (intval($since_start->format('%Y') ) >= 1) {
echo $year = $since_start->format('%Y years ago');
} else if (intval($since_start->format('%m')) >= 12) {
echo $months = $since_start->format('%m month ago');
} else if (intval($since_start->format('%a')) >= 1) {
echo $days = $since_start->format('%a days ago');
} else if (intval($since_start->format('%h')) >= 1) {
echo $hourss = $since_start->format('%h hours ago');
} else if (intval($since_start->format('%i')) >= 1) {
echo $min = $since_start->format('%i minuts ago');
} else if (intval($since_start->format('%s')) >= 1) {
echo $min = $since_start->format('%s seconds ago');
}
}

Your this line of code :
else if(intval($since_start->format('%m')) >= 12){
It says if the month > = 12, then show months ago, but you just have 2 months.
So you should consider changing it to :
else if(intval($since_start->format('%m')) >= 1){

Related

How do i convert the following hours in to minutes? [duplicate]

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

Date Calculation Total Days

I have this code which is working fine for some dates which I don't understand why, below code should calculate the total days. If I select leavefrom = 2014-04-21 and leaveto = 2014-05-02, it gives me total of 8 days but it should be 9 nine days.
Here is the calendar:-
function total_day($leavefrom, $leaveto){
$start_date=strtotime($leavefrom);
$cur_day=$start_date;
$end_day=strtotime($leaveto);
$count=0;
$holiday=array("2014-05-01"=>"Labour Day", "2014-08-31"=>"Independence Day", "2014-12-25"=>"Christmas");
while(1){
//echo date("Y/m/d", $cur_day)."<br/>";
$cur_day=$cur_day +(3600*24);
//echo $count."S--".date("Y-m-d", $cur_day)."<-----E--".$end_day;
$day_of_week=date('w', $cur_day);
//echo "day_of_week-----".$day_of_week."<br/>";
if ($day_of_week == 0 || $day_of_week == 6) {
//No Operation
}else if(array_key_exists(date("Y-m-d", $cur_day), $holiday)){
//echo "Holiday because of ".$holiday[date("Y-m-d", $cur_day)];
}else{
$count++;
}
//echo "Total day--".$count."<br/><br/>";
if(($cur_day==($end_day+(3600*24)))||($cur_day>$end_day)){
break;
}
}
//$count = $count + 1;
return $count;
}
$totaldays = total_day($leavefrom, $leaveto);
I see no error there. From 2014-04-21 to 2014-05-02 there are 11 days out of which 2 weekends and 1 holiday (2014-05-01). Thus making 11 days - 3 days = 8 days
But If you want to include the starting date as well. Then you can use this:
while(1){
if($cur_day>$end_day){
break;
}
$day_of_week=date('l', $cur_day);
if (in_array($day_of_week,array('Saturday','Sunday'))) {
// NO operation
}else if(array_key_exists(date("Y-m-d", $cur_day), $holiday)){
// NO operation
}else{
$count++;
}
$cur_day=$cur_day +(3600*24);
}

calculate time difference like facebook messaging system [duplicate]

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";
}
}

How do I check if something was 4 days ago in PHP?

I am trying to write a function which checks if a "Finished Lesson" was four days ago. How do I check if said lesson was in that time range, for example. If it was finished yesterday, 2 days ago, 3 days ago, 4 days ago, it would be true since it is in the time range of "4 days ago".
How do I check this?
So far I've done:
$time = time();
$fourDays = 345600;
$threeDays = 259200;
$lastLesson = $ml->getLesson($cid, $time, true);
$lastLessonDate = $lastLesson['deadline'];
$displayLastLesson = false;
if ($lastLessonDate + $fourDays < $time)
{
$displayLastLesson = true;
//We print lesson that was finished less than 4 days ago
}
else
{
//We print lesson that is in the next 3 days
}
Right now, the if statement keeps hitting true which is not what I want since I have a lesson that was finished on the 3rd May. It should be true for a lesson that was finished on the 7th May I guess?
$time = time();
$fourDays = strtotime('-4 days');
$lastLesson = $ml->getLesson($cid, $time, true);
$lastLessonDate = $finishedLesson['deadline'];
$displayLastLesson = false;
if ($lastLessonDate >= $fourDays && $lastLessonDate <= $time)
{
$displayLastLesson = true;
//We print lesson that was finished less than 4 days ago
}
else
{
//We print lesson that is in the next 3 days
}
All calculations should be calculated relative to today at 12am, not time() which gives you the current time now (e.g. 6pm) This is an issue because when you do this, 1 day ago (now - 24hours) means time that is between yesterday 6pm and today 6pm. Instead, yesterday should mean a time between yesterday 12am and today 12am.
Below is a simplified calculation to illustrate the idea:
$lastLessonDate = strtotime($lastLessonDate);
$today = strtotime(date('Y-m-d')); // 12:00am today , you can use strtotime('today') too
$day = 24* 60 * 60;
if($lastLessonDate > $today) // last lesson is more than 12:00am today, meaning today
echo 'today';
else if($lastLessonDate > ($today - (1 * $day))
echo 'yesterday';
else if($lastLessonDate > ($today - (2 * $day))
echo '2 days ago';
else if($lastLessonDate > ($today - (3 * $day))
echo '3 days ago';
else if($lastLessonDate > ($today - (4 * $day))
echo '4 days ago';
else
echo 'more than 4 days ago';

How to calculate person's age in months+days in PHP 5.2?

I have asked this question before and accepted the answer but now I found that the php version on our server is 5.2 and DateTime::diff() is not working on that.
I want to calculate person's age in months plus days using date of birth and a given date.
Date Format Input: Y-m-d (example: 1986-08-23)
Output:
5 months and 20 days old.
150 months and 4 days old.
285 months and 30 days old.
Thanks
Here's a solution that'll accurately determine the number of months and number of days, including leap years. It assumes that things like July 21 to August 21 is 1 month 0 days, not 1 month 1 day, and that March 21 to April 20 is 0 months 30 days, not 1 month 0 days. The latter in both cases is what occurs when you just do a straight divide by 30 to calculate months.
I'm sure there's a better way to optimize the function, but it gets the job done:
function diff_date($start_date, $end_date) {
list($start_year, $start_month, $start_day) = explode('-', $start_date);
list($end_year, $end_month, $end_day) = explode('-', $end_date);
$month_diff = $end_month - $start_month;
$day_diff = $end_day - $start_day;
$months = $month_diff + ($end_year - $start_year) * 12;
$days = 0;
if ($day_diff > 0) {
$days = $day_diff;
}
else if ($day_diff < 0) {
$days = $end_day;
$months--;
if ($month_diff > 0) {
$days += 30 - $start_day;
if (in_array($start_month, array(1, 3, 5, 7, 8, 10, 12))) {
$days++;
}
else if ($start_month == 2) {
if (($start_year % 4 == 0 && $start_year % 100 != 0) || $start_year % 400 == 0) {
$days--;
}
else {
$days -= 2;
}
}
if (in_array($end_month - 1, array(1, 3, 5, 7, 8, 10, 12))) {
$days++;
}
else if ($end_month - 1 == 2) {
if (($end_year % 4 == 0 && $end_year % 100 != 0) || $end_year % 400 == 0) {
$days--;
}
else {
$days -= 2;
}
}
}
}
return array($months, $days);
}
list($months, $days) = diff_date('1984-05-26', '2010-04-29');
print $months . ' months and ' . $days . ' days old.';
Output:
314 months and 3 days old.
Edit: I tried to get rid of redundancy in the code, and forgot to rename a variable. This function will now work correctly for diff_date('2010-06-29', '2011-07-01').
Edit: Now correctly works for end months occurring after months with 31 or 28/29 days.
Use your favorite date parsing function (strtotime, strptime, mktime) to get an UNIX timestamp out of the date, then the interval ($now - $then)... and then work out how many seconds there are in a month and use that to calculate how many months the person has lived (division and remainder are your friends).
This'll give you a mathematically precise value that should be close enough to real life too.

Categories