from INT to time in PHP - php

I'm out of ideas here.
In my DB table, I have all sorts of entries and they all have a row with start_time and duration. In this situation, the only way I could've entered the values was:
10:00 is entered in the db as 1000, and 23:00 is entered as 2300.
The durations are 130 for 1 and a half hours or 20 for twenty minutes and so on.
They are entered just as you would expect them to be without any symbols separating the digits.
The problem is, I just can't add the numbers and need to get the end_time calculated by adding the two values.
As an example, If i have an event starting at 1045 and it lasts 45 , I just need to echo 1130.

I'll gladly change it to 0900
You can try following:
$timed = strtotime("+".$duration." minutes", strtotime($time));
echo date('Hi', $timed);
First you need to convert the time and add your duration. After that, you can echo it as desired.
For eg.
$timed = strtotime("+130 minutes", strtotime("0900"));
echo date('Hi', $timed);
It will output
1110

Related

Laravel get total time workerd by employee between two dates using carbonInterval or carbon php

i have time shifts which are assigned to the user. Suppose a night shift starting time is 21-00-00 pm of one july and its ending time is 03-00-00 am of 2nd July. Now i want to get total time a employee worked by adding start time to end time which is equal to 6 hours and i should get six hours. I have tried following code which is working fine for current date like it will give me exact 6 hours if start time is equal to 15-00-00 pm of 1 july to 21-00-00 pm of 1 july but it will fail when shifts exists between two dates as i mentioned above.
$attendance_start_time = \Carbon\Carbon::parse($shift->start_time);
$attendance_end_time = \Carbon\Carbon::parse($shift->end_time);
$total_attendance_time=$attendance_end_time->diffInSeconds($attendance_start_time,true);
\Carbon\CarbonInterval::seconds($total_attendance_time)->cascade()->forHumans()
i am expecting six hours but it is giving me following result
18 hours
i want exact six hours
Not sure if it will fully solve your problem, but check out this :
\Carbon\CarbonInterval::seconds(4100)->cascade()->forHumans(['aUnit' => true]);
UPD:
It might be this solution will work in your case, but make sure that you have tested all of the edge-cases:
$startTime = \Carbon\Carbon::parse('2022-07-02 19:00');
$endTime = \Carbon\Carbon::parse('2022-07-02 19:30');
$diff = $startTime->diffInSeconds($endTime);
if ($endTime->greaterThanOrEqualTo($endTime) && ! $endTime->isSameDay($startTime)) {
$diff = $startTime->diffInSeconds($endTime->addDay());
}
$humanReadable = \Carbon\CarbonInterval::seconds($diff)->cascade()->forHumans(['aUnit' => true]);

Go over 24 hours in a date?

I am working on project (a Google Transit feed) where I am required to provide the times for each stop on a bus route in the following common format: 21:00:00 and so forth.
Problem is, if times continue past midnight for a given trip, they require it to continue the hour counting accordingly. They explain quite specifically that 02:00:00 should become 26:00:00 and 03:45:00 should become 27:45:00 etc.
I am baffled on how to display such with any of the date() or strtotime() functions.
The only thing I can think of in my particular situation would be to function match and replace any strings in my output between 00:00:00 and 04:00:00, as that would clearly mean (again, for me only) that these are trips originating before midnight, but I don't feel that's the correct way.
Well seeing as it's only displaying on the page, you can
firstly get your date from where ever
Let's say $date = 00:00:00
$exploded_date = explode(":", $date);
This takes $date and puts it into an array so
$exploded_date[0] is hh
$exploded_date[1] is mm
$exploded_date[2] is ss
Then what you can do is use ltrim() to remove the leading 0 from 00 to 04 $exploded_date[0] - This makes it comparable in the if statement I'll do after
if($exploded_date[0] <= 4) {
$exploded_date[0] = ltrim($exploded_date[0], "0");
$exploded_date[0] = $exploded_date[0]+24;
}
Then you can implode the array back together into one string
$date = implode(":", $exploded_date);
// if the hour is 00 to 04 it will come out as 24 to 28
// e.g. 24:35:30
echo $date;
Despite giving you an answer. It's a silly thing to be doing, but it's not your choice so here you go :)
The way you display something doesn't necesarily has to be the same way you store something.
I don't know how you calculate the times, but assuming you have a start date and time, and some interval, you could calculate the end time as follows:
date_default_timezone_set('Europe/London');
$start_datetime = new DateTime('2014-11-11T21:00:00');
$next_stop = new DateTime('2014-11-12T02:00:00');
echo $start_datetime->format('Y-m-d H:i'); // 2014-11-11 21:00
echo $next_stop->format('Y-m-d H:i'); // 2014-11-12 02:00
$interval = $start_datetime->diff($next_stop);
// display next stop: 2014-11-11 26:00
echo ($start_datetime->format('Y') + $interval->y) .'-'
. ($start_datetime->format('m') + $interval->m) .'-'
. ($start_datetime->format('d') + $interval->d) .' '
. ($start_datetime->format('H') + $interval->h) .':'
. ($start_datetime->format('i') + $interval->i);
What I'm doing: create the start date (& time) and the datetime of the next stop. With the DateTime::diff() function I'm calculating the difference, and then, only for display (!) I add up each year, month, day, hour and minute to the datetime year, month etc. of the next stop.
This way you can still store your dates and times in a way every human being and computer system will understand (because let's be honest; to represent a time as 27:45 PM is quite ridiculous...)
I don't know if you only want the hours to be added up and roll over the 24 hour, or also days in a month etc. It's up to you how you handle these cases. Good luck!

Transfer time from MySQL database to php countdown

I am currently setting up a ban's list for a server I run for the Garry's Mod game.
When I ban someone on the Garry's Mod server it uploads information about the ban onto a MySQL database (as shown below).
I have noticed that it is writing the 'Length' time as a rather long, complicated number which I have no idea about (apart from the '0' number which is a permanant ban).
Some how PHP is writing this number into a date and time format:
<?php if ($row['Length'] == '0')
{
echo "Permanent";
}
elseif($row['Length'] < time())
{
echo "Expired";
}
else
{
echo date("g:ia - d M, Y", $row['Length']);
}
?>
Which ends up looking a little something like this:
This system above is working perfectly well at showing the date when a ban is due to expire.
My main issue is that I would like it to display the time remaining until the ban expires in a hour format (eg. Ban expires in: 67 Hours, 29 Minutes.
This 'complicated number' is Unix timestamp (the number of seconds since January 1 1970 00:00:00). You can for example subtract value of column Length from column Time and calculate from that number of hours and minutes.
You can try
// For example, if you have a +2 timezone, you can do this:
$TimeEDITED = gmdate("H:i:s", time() + 7200);
// ... if you do not:
$TimeNONEDITED = gmdate("H:i:s", time());

Counting down days not showing the right number of days

I need help.. Is this right?
Start Date: Mar 16, 2014
End Date: Mar 19, 2014
Results: 2 Days
$plantEnd = get_the_author_meta('plantEnd', $sellerID );
$plantStart = get_the_author_meta('plantStart', $sellerID );
$future = $plantEnd;
$d = new DateTime($future);
echo $d->diff(new DateTime())->format('%a').' Days';
Why does it says 2 days? Isn't it 3 days? Im confused..
Since you aren't actually using $plantStart in your code and instead using the current time, you're basically getting a difference between now (the time the script was run, on server's time zone) and the start of Mar 19, 2014 (0h:0m:0s). So what you are really getting is something like 2 days 5 hours 3 minutes 25 seconds (depending on when you run it vs. server time.
for example, when I run this locally:
$d->diff(new DateTime())->format('%d:%H:%i:%s');
I get 2:04:59:25
So there's more to it than just getting that "2" returned.. you're just not formatting for it.
And again, you aren't actually using the $plantStart anywhere either. So if you were to do this:
<?php
$plantEnd = '2014-03-19';//get_the_author_meta('plantEnd', $sellerID );
$plantStart = '2014-03-16'; //get_the_author_meta('plantStart', $sellerID );
$future = $plantEnd;
$d = new DateTime($future);
echo $d->diff(new DateTime($plantStart))->format('%d:%H:%i:%s');
?>
You will see it outputs 3:00:0:0 (or you could continue to just use %d and get the "3"). This is because $plantStart (presumably - based on your post) just specifies yyyy-mm-dd, so passing just the yyyy-mm-dd value will put the hh:mm:ss at 0:0:0 (beginning of day) , so it will be a full day's calculation, which has the effect of "rounding up" to the whole day increment.
I have a feeling that it's actually 2 days, someodd hours, and someodd minutes, or something to that effect. Because you're formatting to just do days, you're losing the nuances. I'd change the code to say "2.4 days" (and for the life of me I can't remember how I did this in the past...)
EDIT: in the past I have simply used date() instead of DateTime().
I did a little research, and you might want format('%d')." Days";

What is the best way for subtracting dates to see what time is left in php?

What is the best way for subtracting dates to see what time is left to some fixed date event? For example, I want to display how many days are left for some distant future event in 2013-10-14.
Ok
$now=strtotime(date("Y-m-d"));
$fix=strtotime(date("2013-10-14"));
$left=$fix-$now;
$left=date('d', $left);
echo "There are ".$left." days left";
The problem is: it displays that 01 day is left. I think I didn't formated timestamp into data format well. Am I right?
Going off of what Niklas said, you can convert your times to strings. Your problem is the line $left=date('d', $left). The d in date returns the day of the month, and must be a value between 1 and 31.
You could just get the value in seconds and then use some math to get the correct amount of days, like so:
$now=strtotime(date("Y-m-d"));
$fix=strtotime(date("2013-10-14"));
$secondsLeft=$fix-$now;
$daysLeft = $secondsLeft / 24 / 60 / 60;
echo "There are ".$daysLeft." days left.";
Which outputs "There are 89 days left." July 17 plus 89 days does indeed equal October 14.

Categories