Please show some code (migration, data insertion & data sample)
Time 24H format
I am inserting time in database the only issue is when I choose 12:00:00 AM it's converting to 00:00:00 , can I convert it to 24:00:00 instead ?
@folium This site is not working
there is no such time as 24:00:00
@Snapey Actually in phpMyadmin I can insert 24:00:00 but manually, the reason I want this is to calculate the total based on the time if it's $heure< 24:00:00 or $heure > 24:00:00
Let's take an example if $taux->nbrejr >=3 and $taux->heurdep > '00:00:00' the amount calculated is
$frais->Dejeuner + $frais->Diner + $frais->Decouch and not $frais->Decouch because 00:00:00 < 12:00:00, I want to prevent this confusion
` if( $taux->nbrejr == 1)
{
if( ($taux->heuredep <= '12:00:00') && ($taux->heureret > '12:00:00'))
{
echo $frais->Dejeuner ;
}
}
elseif( $taux->nbrejr == 2)
{
if($taux->heuredep <= '12:00:00')
{
echo $frais->Dejeuner + $frais->Diner + $frais->Decouch;
}
elseif ( ($taux->heuredep > '12:00:00') && ($taux->heuredep <= '00:00:00'))
{
echo $frais->Diner + $frais->Decouch;
}
elseif( $taux->heuredep > '00:00:00' )
{
echo $frais->Decouch;
}
}
elseif( $taux->nbrejr >= 3)
{
if( $taux->heuredep <= '12:00:00')
{
echo $frais->Dejeuner + $frais->Diner + $frais->Decouch;
}
elseif(($taux->heuredep > '12:00:00') && ($taux->heuredep <= '00:00:00'))
{
echo ($frais->Diner + $frais->Decouch);
}
elseif( ($taux->heuredep > '00:00:00'))
{
echo $frais->Decouch;
}
}
`
there is no such time as 24:00:00
@Snapey I'm sure 24:00:00 turns into 00:00:00, as 24:00:00 doesn't exist as a time - unless for social reasons on paper I guess.
I think you can set it on some computers where it goes to 24:23 and then it goes to 01:00 - but that's not how the military clock works by default.
@Snapey So what should I do now ? '00:00:00' isn't detected as '12:00 AM'
@arinaseeuu you have bigger problems than that, you cannot compare times like this;
$taux->heuredep <= '12:00:00'
@Snapey and how then can I compare them ?
@arinaseeuu You can use Carbon to easily manipulate dates in a Laravel application.
Here you have some comparison methods you can use with a Carbon instance : https://carbon.nesbot.com/docs/#api-comparison
@arinaseeuu convert them to Carbon instances and then use the comparison methods
@Snapey is this the right method to convert them to Carbon instance ?
\Carbon\Carbon::parse($taux1->heuredep )->format('H:i'); \Carbon\Carbon::parse($taux1->heureret )->format('H:i');
I think I converted the time to carbon instance but when I compare it with eq() method I get this error.
Call to a member function eq() on string
$date1 = \Carbon\Carbon::parse($taux1->heuredep )->format('H:i');
$date2 = \Carbon\Carbon::parse($taux1->heureret )->format('H:i');
$result = $date1->eq($date2);
dd($result);`
@arinaseeuu remove the format (changes it back from carbon into a string)
@arinaseeuu the format() method you use returns a string. You don't have to format your Carbon instances to compare them. You have to do something like this :
$date1 = \Carbon\Carbon::parse($taux1->heuredep);
$date2 = \Carbon\Carbon::parse($taux1->heureret);
$result = $date1->eq($date2);
dd($result);
@anthony-ab , @snapey okay so now if I want to compare $date1 with '12:00:00' or any time I should do this ?
$date1 = \Carbon\Carbon::parse($taux1->heuredep);
$date = \Carbon\Carbon::parse('12:00:00');
$result = $date1->eq($date);
is this right ? but it will be so long to parse every time I want, since I have to check if the time is greater than or lower that 12:00:00 or 18:30:00 or 00:00:00 then calculate the amount
I don't know if this is right or not, please check this
$heuredep = \Carbon\Carbon::parse($taux1->heuredep );
$heureret = \Carbon\Carbon::parse($taux1->heureret );
$time1 = \Carbon\Carbon::parse('12:00:00');
$time2 = \Carbon\Carbon::parse('00:00:00');
$time3 = \Carbon\Carbon::parse('18:30:00');
if($taux1->nbrejr ==1){
if( ($heuredep->lte($time1) )&& ($heureret->gt($time1)) )
$total = $frais1->Dejeuner;
}
elseif($taux1->nbrejr == 2){
if( $heuredep->lte($time1))
$total = $frais1->Dejeuner + $frais1->Diner + $frais1->Decouch;
elseif ( ($heuredep->gt($time1)) && ($heuredep->lte($time2)))
$total = $frais1->Diner + $frais1->Decouch;
elseif( $heuredep->gt($time2))
$total = $frais1->Decouch;
}
elseif($taux1->nbrejr == 3){
if( $heuredep->lte($time1))
$total = $frais1->Dejeuner + $frais1->Diner + $frais1->Decouch;
elseif( ($heuredep->gt($time1)) && ($heuredep->lte($time2) ))
$total = $frais1->Diner + $frais1->Decouch;
elseif( $heuredep->gt($time2))
$total = $frais1->Decouch;
}
// //heureret
if( $taux1->nbrejr == 1)
{
if( $heureret->gt($time3) && $heureret->lte($time2))
$total1 = $frais1->Diner ;
}
elseif( $taux1->nbrejr == 2)
{
if( $heureret->gt($time1) && $heureret->lte($time3) )
$total1 = $frais1->Dejeuner;
else if( $heureret->gt($time3) && $heureret->lte($time2))
$total1 = $frais1->Dejeuner + $frais1->Diner;
else if( $heureret->gt($time2))
$total1 = $frais1->Dejeuner + $frais1->Diner + $frais1->Decouch;
}
elseif( $taux1->nbrejr >=3 )
{
if( $heureret->gt($time1) && $heureret->lte($time3) )
$total1 = $frais1->Dejeuner;
elseif( $heureret->gt($time3) && $heureret->lte($time2))
$total1 = $frais1->Diner + $frais1->Dejeuner;
elseif( $heureret->gt($time2))
$total1 = $frais1->Dejeuner + $frais1->Diner + $frais1->Decouch;
}
the condition inside if statement is working when I dumped it, it return false or true ; the correct answer, but I keep getting this errors compact(): Undefined variable: total1 and compact(): Undefined variable: total even though that both of these variables are defined inside if loop
Never let variables unset.
at the beginning of your code, $total=-1; $total1=-1; and your calculations should conduct to positive variable, a return of -1 indicate that you don't go through the if statements.
@sr57 Hello guys, I'm really lost I don't know what to do
First step, as I wrote just before
$total=-1; $total1=-1;
if($taux1->nbrejr ==1){
....
Second, if you need* to know which calculation is done, a dump a each parts of the "if"
*for instance to detail us your pb
Then, resume your question if you still need help.
@sr57 hey, hope everything is going well with you,
I did what you mentioned, the variable $total is returning the right value, but $total1is returning -1,
I parsed 24:00:00 and when I dumped it it returns `00:00:00, and used it to compare it with the time in data base
$time2 = Carbon::parse('24:00:00');
@sr57 When the time is equal to 00:00:00 it returns a negative value
I did what you mentioned, the variable $total is returning the right value, but $total1is returning -1
Clear and confirm you have a pb of logic/code.
next
not clear.
I parsed 24:00:00
which variable?
when I dumped it it returns `00:00:00
which variable? where in your code?
the time in data base $time2 = Carbon::parse('24:00:00');
It's a php constant, not a db variable
When the time is equal to 00:00:00
which variable?
it returns a negative value
which variable?
It's probably clear in your mind, but no reader is in your mind.
So 3 things
-1- Always reread your post and avoid what I wrote before (name the variable, share code with your dump et give the result in comment)
-2- Simplify your code at minimum that explains the pb you get.
-3- Explain the pb by giving, the minimum code, the input(s) (ideally one to explain the pb) , the result and the expected result.
@sr57 Here's my updated code, I fixed the 24 problem but now it's giving me another one, comparison methods were working perfectly but I'm getting this error Call to a member function lte() on string.
Here's the value of each variable when I dumped them so you can understand :
$start = ' 2022-05-15 08:08:00.0 UTC (+00:00)'
$heuredep = ' 08:08:00'
$end = '2022-05-15 00:15:00.0 UTC (+00:00)'
$heureret = ' 00:15:00'
$heureret2 = '24:15:00' // I converted $heurret to 24 format
$midday = '2022-05-15 12:00:00.0 UTC (+00:00)'
$time1 = '12:00:00'
$sixpm = '2022-05-15 18:30:00.0 UTC (+00:00)'
$time = '18:30:00'
$midnight = '2022-05-16 00:00:00.0 UTC (+00:00)'
$time = '00:00:00'
$time2 = '24:00:00' // I converted $time to 24 format again
I did this conversion to prevent the confusion of 00:00:00 , I dumped all these variables above and they're getting the correct value, but why does the method not accepting them now ? When I've already done the same thing before and I just changed the format now??
$start = \Carbon\Carbon::parse($taux1->heuredep );
$heuredep = $start->format('h:i:s');
// dd($heuredep);
$end = \Carbon\Carbon::parse($taux1->heureret );
$heureret = $end->format('H:i:s');
// dd($heureret);
$date = Carbon::parse($heureret);
if ($date->format('H:i:s') >= '00:00:00') {
$heureret2 = $date->copy()->subDay()->format('24:i:s');
}
// dd($heureret2);
$midday = \Carbon\Carbon::parse('12:00:00');
$time1 = $midday->format('h:i:s');
// dd($midday);
$sixpm = \Carbon\Carbon::parse('18:30:00');
$time3 = $sixpm->format('H:i:s');
// dd($time3);
$midnight = Carbon::parse('24:00:00');
$time = $midnight->format('H:i:s');
// dd($time2);
$date00 = Carbon::parse($time);
if ($date->format('H:i:s') >= '00:00:00') {
$time2 = $date00->copy()->subDay()->format('24:i:s');
}
$total=-1;
$total1=-1;
if($taux1->nbrejr ==1){
if( ($heuredep->lte($time1) == "true" ) && ($heureret2->gt($time1) == "true") )
$total = $frais1->Dejeuner;
}
elseif( $taux1->nbrejr == 2)
{
if( $heuredep->lte($time1) == "true")
$total = $frais1->Dejeuner + $frais1->Diner + $frais1->Decouch;
elseif ( ($heuredep->gt($time1) == "true") && ($heuredep->lte($time2) == "true"))
$total = $frais1->Diner + $frais1->Decouch;
elseif( $heuredep->gt($time2) == "true")
$total = $frais1->Decouch;
}
elseif( $taux1->nbrejr >= 3 )
{
if( $heuredep->lte($time1) == "true")
$total = $frais1->Dejeuner + $frais1->Diner + $frais1->Decouch;
elseif( ($heuredep->gt($time1) == "true") &&($heuredep->lte($time2) == "true") )
$total = $frais1->Diner + $frais1->Decouch;
elseif( $heuredep->gt($time2) == "true")
$total = $frais1->Decouch;
}
//heureret
if( $taux1->nbrejr == 1)
{
if(( $heureret2->gt($time3) == "true" )&& ($heureret2->lte($time2) == "true"))
$total1 = $frais1->Diner ;
}
elseif( $taux1->nbrejr == 2)
{
if( ($heureret2->gt($time1) == "true") && ($heureret2->lte($time3) == "true") )
$total1 = $frais1->Dejeuner;
elseif( ($heureret2->gt($time3) == "true") && ($heureret2->lte($time2) == "true"))
$total1 = $frais1->Dejeuner + $frais1->Diner;
elseif( $heureret2->gt($time2) == "true")
$total1 = $frais1->Dejeuner + $frais1->Diner + $frais1->Decouch;
}
elseif( $taux1->nbrejr >=3 )
{
if( ($heureret2->gt($time1) == "true" ) && ($heureret2->lte($time3) == "true" ))
$total1 = $frais1->Dejeuner;
elseif( ($heureret2->gt($time3) == "true") && ($heureret2->lte($time2) == "true"))
$total1 = $frais1->Diner + $frais1->Dejeuner;
elseif( ($heureret2->gt($time2) == "true" ))
$total1 = $frais1->Dejeuner + $frais1->Diner + $frais1->Decouch;
}
lte works with Carbon date and your "dates" are string ( ex $time1 = '12:00:00' )
use
$time1 = \Carbon\Carbon::createFromFormat('m/d/Y H:i:s', '01/01/2022 10:20:00');
or for today
$time1 = \Carbon\Carbon::createFromFormat('H:i:s', '10:20:00');
and not that $time1 = \Carbon\Carbon::createFromFormat('H:i:s', '24:00:00');
is tomorrow at 00:00:00, so you can use it for your comparisons.
That said, if you fixed the 24 problem, don't you think it's time to close this thread.
@sr57 the problem is solved, thank you everyone
For those who are facing the same problem remove the comparison method and compare normally ( >, ...)
PS : read the last code that I inserted
@sr57 I know it's not the best solution, I did that to submit the assignment in time. I'm trying now to figure out how to use carbon methods.
Please or to participate in this conversation.