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';
Related
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 am trying to determine if a day and time are between two others, I have the following...
$currentdate = date("N h:i:s A");
This returns the day of the week as a number and then the current time in 24 hour format.
I want to check if the $currentdate is between 9am on a Friday and 9am on a Monday.
What is the best way to tackle this?
I believe this should give you what your asking for but I'm sure there are better ways to implement. So basically the time has been converted into an INT for comparing and the hours are configured not to have a leading zero hence why $timeOne and $timeTwo is shorter. I've then used an if statement to test days and time on that specific day leaving you a slot to add your code if those conditions are met.
function checkDayTime() {
$day = date(w); //0 (for Sunday) through to 6 (for Saturday)
$timeOne = 90000;
$timeTwo = 90000;//Added for easier reading
$currentTime = (int) date('Gis'); //Time as INT 00000 > 240000
if (($day == 5 && $currentTime > $timeOne) || ($day == 6 || $day == 0) || ($day == 1 && $currentTime < $timeTwo)) {
//Between those hours
return TRUE;
} else {
//Not between those hours
return FALSE;
}
}
Just removed the extra if statement as it was not needed
$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){
I have the following function that I wrote couple of years ago. It takes a datetime from my db and displays it in a better formatted way.
function formatTime($dateTime){
// show time only if posted today
if (date('Ymd') == date('Ymd', strtotime($dateTime))) {
$dt = date('g:i a', strtotime($dateTime));
} else {
// if not the same year show YEAR
if (date('Y') == date('Y', strtotime($dateTime))) {
$dt = date('M j', strtotime($dateTime));
} else {
$dt = date('M j, Y', strtotime($dateTime));
}
}
return $dt;
}
I use server time, which is CST for me. Yesterday I had a user from Australia pointing out that for him it did not make any since since he way on an entirely different time zone, actually a day ahead (when compared to my output at certain time :).
I decided to rewrite my function to say something like:
if under a minute > seconds ago
if under an hour > # minutes ago
between 1 -2 hrs > over an hour ago
2 - 24 hrs > day ago
2 - 7 days > # days ago
7 days - month > # weeks ago
1 - 2 months > over a month
after that I can just show a date
Are there any functions that you are perhaps aware of doing this, if not how would I modify this one?
Thanks.
function formatTime ($dateTime) {
// A Unix timestamp will definitely be required
$dateTimeInt = strtotime($dateTime);
// First we need to get the number of seconds ago this was
$secondsAgo = time() - $dateTimeInt;
// Now we decide what to do with it
switch (TRUE) {
case $secondsAgo < 60: // Less than a minute
return "$secondsAgo seconds ago";
case $secondsAgo < 3600: // Less than an hour
return floor($secondsAgo / 60)." minutes ago";
case $secondsAgo < 7200: // Less than 2 hours
return "over an hour ago";
case $secondsAgo < 86400: // Less than 1 day
return "1 day ago"; // This makes no sense, but it is what you have asked for...
case $secondsAgo < (86400 * 7): // Less than 1 week
return floor($secondsAgo / 86400)." days ago";
case $secondsAgo < (86400 * 28): // Less than 1 month - for the sake of argument let's call a month 28 days
return floor($secondsAgo / (86400 * 7))." weeks ago";
case $secondsAgo < (86400 * 56): // Less than 2 months
return "over a month ago";
default:
return date('M j, Y', $dateTimeInt);
}
}
This is by no means flawless, especially since one of your requirements doesn't make sense (see comments) but hopefully it should give you a push in the right direction, and illustrate how you can use switch to allow you to easily add and remove items/options from the behaviour.
I am using the following code to calculate the days remaining to make edits. They have 30 days to make edits, and days count down, this code works perfectly.
<?php
// Calculate days remains to edit or change details
$today = time();
$cdate = strtotime($row_details['payment_date']);//strtotime("19:19:09 Sep 27, 2011");
$dateDiff = $today - $cdate;
$fullDays = floor($dateDiff/(60*60*24));
$dayscalculate = 30 - $fullDays; // Set number of days
echo $dayscalculate.(($dayscalculate == 1) ? " day" : " days");
//
?>
QUESTION: if days = say 3 will say 3 days.. but if days = 0 (is the last of the 30 days).. Then want to say this is your last day or something.. So need an if based on $dayscalculate..
Ideas?
Thank you
How about...
if ($dayscalculate == 0) {
echo 'This is your last day';
} else {
printf('%d day%s',
$dayscalculate,
$dayscalculate > 1 ? 's' : ''
);
}
You may also want to introduce an "out of time" check, ie $dayscalculate < 0 but then again, you may already be handling this scenario.