Here i am doing remaining count town time, Like suppose event going to start 2018-04-18 04:30 PM,Suppose current time is 2018-04-18 04:10 PM means i want to display like 20 Minutes left,I witten the code but it is showing wrong result
My PHP code:
<?php
date_default_timezone_set('UTC');
date_default_timezone_set('Asia/Kolkata');
function timeAgo($logintime)
{
$start_date = new DateTime($logintime);
$since_start = $start_date->diff(new DateTime(date("Y-m-d h:i:s")));//2018-04-18 04:10 PM
if( intval($since_start->format('%Y') ) >= 1){
$timeago = $since_start->format('%Y years');
}
else if(intval($since_start->format('%m')) >= 1){
$timeago = $since_start->format('%m months ');
}
else if(intval($since_start->format('%a')) >= 1){
$timeago = $since_start->format('%a days ');
}
else if(intval($since_start->format('%h')) >= 1){
$timeago = $since_start->format('%h hours ');
}
else if(intval($since_start->format('%i')) >= 1){
$timeago = $since_start->format('%i minutes ');
}
else if(intval($since_start->format('%s')) >= 1){
$timeago = $since_start->format('%s seconds ');
}
return $timeago;
}
echo timeAgo('2018-04-18 04:30 PM');
?>
I am getting result like this
12 hours
My Expected output is
20 Minutes left
Your just give the now DateTime only. You don't want to declare the time format. And you just use this time zone only date_default_timezone_set('Asia/Kolkata');. Here is code:
date_default_timezone_set('Asia/Kolkata');
function timeAgo($logintime)
{
$start_date = new DateTime($logintime);
$since_start = new DateTime();
$interval = $since_start->diff($start_date);
$timeago = $interval->format("%a days, %h hours, %i minutes, %s seconds");
if($since_start < $start_date){
return $timeago. ' ago';
}else{
return 'This Event Passed';
}
}
echo timeAgo('2018-04-19 05:50 PM');
Out put:
1 days, 0 hours, 56 minutes, 28 seconds ago
Related
I need to calculate end-date/SLA for a given start-date considering the completion-time in minutes within the business hours/days.
For instance :
start-date = 2019-03-29 15:00:00
completion-time = 720 (minutes)
Considering 8 working hours per day(9am to 5pm) and weekend on Sunday. The end-date should be 2019-04-01 11:00:00.
So the total hours are 12,
on 2019-03-29 from 15:00:00 to 17:00:00 (2 hours)
on 2019-03-30 from 09:00:00 to 17:00:00 (8 hours)
on 2019-04-01 from 09:00:00 to 11:00:00 (2 hours)
Any help with regards to this would be much appreciated.
So far i can able to get the total working days excluding the weekends but getting exact end time is where i'm struggling.
Thank you.
Update:
$bookingDateTime = Carbon::parse('2019-03-29 15:00:00');
$i = 0;
$completion_in_days = (720/60)/8; //converted minutes in no of days
$working_days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
while($i < ($completion_in_days)){
if(array_search(strtolower($bookingDateTime->englishDayOfWeek), $working_days) !== false){
$i++;
}
$bookingDateTime->addDay();
}
This will give me the end date.
Please find the snippet below, I wrote line by line commenting for understanding
$endDate = $startDate = '2019-03-29 15:00:00';
$officeStart = '09:00:00';
$officeEnd = '17:00:00';
$totalHours = 12;
$i = 1;
$flag = false;
while ($totalHours > 0) {
$day = date('D', strtotime($endDate)); // fetching day of week
if ($day == 'Sun') { // checking if sunday thenskip by adding 1 day to end date
$endDate = date('Y-m-d', strtotime($endDate . " +1 Day")) . ' ' . $officeStart; // adding one day if sunday
continue;
}
$diff = strtotime($officeEnd) - strtotime(date("H:i:s", strtotime($endDate))); // getting difference of time of office end date and result end date
$hours = $diff / (3600); // difference in minutes
if ($hours > $totalHours) {
$hours = $totalHours;
$flag = true; // to break loop if last loop comes
} else {
$totalHours = $totalHours - $hours; // substracting hours from total hours left
}
$endDate = date('Y-m-d H:i:s', strtotime("+$hours Hour", strtotime($endDate))); // adding hours which are substracted
if (!$flag) {
$endDate = date('Y-m-d', strtotime($endDate . " +1 Day")) . ' ' . $officeStart; // if not last loop add day to result end date
} else {
break;
}
}
Output
2019-04-01 11:00:00
Demo.
I am creating PHP function that will return difference between two dates in a format: 2 Months, 3 Weeks, 6 Days, 3 Hours. I have tried to use PHP DateTime class, but it returns only Months, Days and Hours and I can not find a way to calculate Weeks.
This is my function:
public function DateTimeDifference($FromDate, $ToDate) {
$FromDate = new DateTime($FromDate);
$ToDate = new DateTime($ToDate);
$Interval = $FromDate->diff($ToDate);
$Difference["Hours"] = $Interval->h;
$Difference["Days"] = $Interval->d;
$Difference["Months"] = $Interval->m;
return $Difference;
}
Now, I need $Difference["Weeks"] also included in return data.
EDIT: I know I can divide Days with 7 and get weeks, but this does not result right. For example: 2 Months, 14 Days, 3 Hours - When I divide 14 days with 7 I will get this: 2 Months, 2 Weeks, 14 Days, 3 Hours and now this is not same period.
public function DateTimeDifference($FromDate, $ToDate) {
$FromDate = new DateTime($FromDate);
$ToDate = new DateTime($ToDate);
$Interval = $FromDate->diff($ToDate);
$Difference["Hours"] = $Interval->h;
$Difference["Weeks"] = floor($Interval->d/7);
$Difference["Days"] = $Interval->d % 7;
$Difference["Months"] = $Interval->m;
return $Difference;
}
// this will only work from previous dates
// difference between utc date time and custom date time
function FromUtcToCustomDateTimeDifference($ToDate)
{
// Takes Two date time
$UTC_DATE = new DateTime('now', new DateTimeZone('UTC'));
$UTC_DATETIME = $UTC_DATE->format('Y-m-d H:i:s');
// add your own date time 1
$datetime1 = date_create(UTC_DATETIME);
$datetime2 = date_create($ToDate);
$Interval = date_diff($datetime1, $datetime2);
// Count Number Of Days Difference
$Day = $Interval->format('%a');
if($Day > 1)
{
$Month = $Interval->format('%m');
$Year = $Interval->format('%y');
$Week = (int)($Interval->format('%a')/7);
if($Year<1)
{
if($Day <= 7)
{
return $Day > 1 ? $Day.= " days ago" : $Day .= " day ago";
}
else if($Month<1)
{
return $Week > 1 ? $Week.= " weeks ago" : $Week .= " week ago";
}
return $Month > 1 ? $Month.= " months ago" : $Month .= " month ago";
}
else
{
return $Year > 1 ? $Year.= " years ago" : $Year .= " year ago";
}
}
else
{
return "today";
}
}
I tryed to display added date as 25 minutes ago, Today 5.30 PM etc.
$added_time = strtotime('1 Jan 2016 6:00 AM');
$currentTime = strtotime('1 Jan 2016 7:15 AM'); // probably uses time()
$diff = timespan($time1, $time2);
if($diff < 1 hour){ // how to check 1 hour
//display minutes ago
}
else {
//display added time
}
Conditions
if the time gap is less than 60 minute -> 25 minutes ago
If the time gap is over 60 minute But Today -> Today 6.00AM
If the time gap is over 60 minute But Yesterday -> Yesterday 6.00AM
Else exactly $added_time
How to check the condition for less than 1 hour, today and yesterday?
This is not specifically a CodeIgniter question. I am not sure exactly what you are doing, but this code will get you close.
$Added = new \DateTime(date('Y-m-d H:i:s', strtotime('1 Jan 2016 6:00 AM')));
$Current = new \DateTime(date('Y-m-d H:i:s', strtotime('1 Jan 2016 7:15 AM')));
$Diff = $Added->diff( $Current , FALSE);
$hours = $Diff->format('%H');
$mins = $Diff->format('%I');
if( $Diff->invert == true ){
echo "Some hours $hours and minutes $mins ago ";
}
else if( $Diff->invert == false ){
echo "Some hours $hours and minutes $mins into the future ";
}
References:
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/datetime.diff.php
I tried to find out difference between today date and specific day with format Ymd.
How to check whether specific day is greater than 30 days from today?
For example:
$date1 = '20160315'; // 2016-03-15
$date2 = '20160115'; // 2016-01-15
Try this
$date1=date_create('20160315');
$date2=date_create('20160115');
$diff=date_diff($date1,$date2);
$days = $diff->format("%a");
if($days > 30) do something
So simple...
$date1 = '20160315'; // 2016-03-15
$date2 = date(Ymd); // 2016-01-15
$day_difference = $date1 - $date2
if($day_difference > 30) {
echo 'specific day is greater than 30 days from today';
} else {
echo 'specific day is less than 30 days from today';
}
Try this:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-11-13');
$interval = $datetime1->diff($datetime2);
$int = $interval->format('%R%a');
if($int > +30) {
echo "Greater than 30 days";
} else {
echo "Less than 30 days";
}
For example if I have:
$seconds = 3744000; // i want to output: 43 days, 8 hours, 0 minutes
Do I have to create a function to convert this? Or does PHP already have something built in to do this like date()?
function secondsToWords($seconds)
{
$ret = "";
/*** get the days ***/
$days = intval(intval($seconds) / (3600*24));
if($days> 0)
{
$ret .= "$days days ";
}
/*** get the hours ***/
$hours = (intval($seconds) / 3600) % 24;
if($hours > 0)
{
$ret .= "$hours hours ";
}
/*** get the minutes ***/
$minutes = (intval($seconds) / 60) % 60;
if($minutes > 0)
{
$ret .= "$minutes minutes ";
}
/*** get the seconds ***/
$seconds = intval($seconds) % 60;
if ($seconds > 0) {
$ret .= "$seconds seconds";
}
return $ret;
}
print secondsToWords(3744000);
This is very simple and easy to find days , hours, minute and second in core php :
$dbDate = strtotime("".$yourdbtime."");
$endDate = time();
$diff = $endDate - $dbDate;
$days = floor($diff/86400);
$hours = floor(($diff-$days*86400)/(60 * 60));
$min = floor(($diff-($days*86400+$hours*3600))/60);
$second = $diff - ($days*86400+$hours*3600+$min*60);
if($days > 0) echo $days." Days ago";
elseif($hours > 0) echo $hours." Hours ago";
elseif($min > 0) echo $min." Minutes ago";
else echo "Just now";
An easy way to accomplish this nowadays is using DateTimeImmutable, DateInterval and PHP 5.5.0 or higher:
$seconds = 3744000;
$interval = new DateInterval("PT{$seconds}S");
$now = new DateTimeImmutable('now', new DateTimeZone('utc'));
$difference = $now->diff($now->add($interval))->format('%a days, %h hours, %i minutes');
The result will be:
43 days, 8 hours, 0 minutes
The code adds the seconds to a date and calculates the difference to it. Like this, the seconds are transformed into the specified days, hours and minutes.
Warning 1: Working without UTC - Clock changes
You may not specify the DateTimeZone in the constructor of the DateTimeImmutable object to UTC.
$now = new DateTimeImmutable();
There are regions in this world, where the clock changes on specific days of the year. Most countries in the EU change between a summer- and winter-time for example.
If your date interval overlaps the day on that a clock change occurs and your server is set to the related region for that clock change, the result might change as well. This is best shown with the following example:
$twentyFourHours = new DateInterval('PT24H');
$twentyFiveHours = new DateInterval('PT25H');
//Pacific time changed from summer- to winter-time on that day
$summerToWinter = new DateTimeImmutable('2018-11-04');
If you add 24 hours to the $summerToWinter date, you will get the following result:
$extra24Hours = $summerToWinter->add($twentyFourHours);
echo $summerToWinter->format('y-m-d H:i');
echo $extra24Hours->format('y-m-d H:i');
echo $summerToWinter->diff($extra24Hours)->format('%a days, %h hours, %i minutes');
18-11-04 00:00
18-11-04 23:00
0 days, 24 hours, 0 minutes
As you can see, between 00:00 and 23:00 on that day lay 24 hours, which is technically correct. Because of the clock change the timelap between 02:00 and 03:00 occured twice on that day.
Adding 25 hours will result in this:
$extra25Hours = $summerToWinter->add($twentyFiveHours);
echo $summerToWinter->format('y-m-d H:i');
echo $extra25Hours->format('y-m-d H:i');
echo $summerToWinter->diff($extra25Hours)->format('%a days, %h hours, %i minutes');
18-11-04 00:00
18-11-05 00:00
1 days, 0 hours, 0 minutes
As we can see, 1 day elapsed, that has had 25 hours. If this is applied for the 3744000 seconds from the original question, the result would show:
43 days, 7 hours, 0 minutes
The information, that an elapsed day has had 25 hours, is not shown though.
Also, I was not able to recreate the same effect for a day that changes the clock from winter to summer time, that should only elapse 23 hours.
Warning 2: Working with the raw DateInterval object
Using this code without DateTimeImmutable will cause the wrong output:
$seconds = 3744000;
$interval = new DateInterval("PT{$seconds}S");
$difference = $interval->format('%a days, %h hours, %i minutes, %s seconds');
Now, only the seconds are set in the DateInterval object. $difference would be:
(unknown) days, 0 hours, 0 minutes, 3744000 seconds
I like Ian Gregory's answer the most and upvoted it but thought i'd just simplify it a little bit :
function secondsToWords($seconds)
{
$days = intval(intval($seconds) / (3600*24));
$hours = (intval($seconds) / 3600) % 24;
$minutes = (intval($seconds) / 60) % 60;
$seconds = intval($seconds) % 60;
$days = $days ? $days . ' days' : '';
$hours = $hours ? $hours . ' hours' : '';
$minutes = $minutes ? $minutes . ' minutes' : '';
$seconds = $seconds ? $seconds . ' seconds' : '';
return $days . $hours . $minutes . $seconds;
}