I have an Integer column "duration_temp" that have values represent the duration in minutes, I want to copy those values in another column "duration" of type timestamp, I'm having the problem of how to convert those Int minutes into timestamps format, for example: if a value in Int is set to 4 then I should convert it to yyyy-mm-dd 00:04:00. is there a function that can do that or close from doing that?any suggestion would be appreciate it.
CodePudding user response:
If you mean that you just have minutes and want to make a timestamp from it with current date information, try this (after adding use Carbon\Carbon;
in top of you file):
$minutes = 4;
return Carbon::create(now()->year, now()->month, now()->day, 0, $minutes)->toDateTimeString();
CodePudding user response:
As your integer column duration_temp
is in minutes, you have to convert it to seconds before you can get the expected result.
Take your example :
Int = 4 minutes => Int = 4 * 60 = 240 second
To finish :
date ("Y-m-d H:i:s", 240); // will give you 1970-01-01 00:04:00
CodePudding user response:
If you have a duration in minutes. You could use DateInterval
like this.
$yourDate = new DateTime('2021-01-01 00:00:00');
$durationInMinutes = 4;
$interval = new DateInterval("PT{$durationInMinutes}M");
$yourDate->add($interval);
echo $yourDate->format('Y-m-d H:i:s');
https://www.php.net/manual/en/dateinterval.construct.php
CodePudding user response:
DateTime accepts extensive Relative Formats. This makes possible as an example:
$durationInMinutes = 67;
$date = date_create('2021-01-01 '.$durationInMinutes.' Minutes');
//or $date = new DateTime('2021-01-01 '.$durationInMinutes.' Minutes');
echo $date->format('Y-m-d H:i:s');
//2021-01-01 01:07:00
Also works correctly with negative minute numbers.