0

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.

Dr.Noob
  • 319
  • 6
  • 17
  • 2
    Does this answer your question? [Convert timestamp to readable date/time PHP](https://stackoverflow.com/questions/5213528/convert-timestamp-to-readable-date-time-php) – Romeo Ninov Sep 20 '21 at 10:05
  • 1
    You just have number of minutes and want to convert it into timestamp ? what about the year, month and day ? – Rateb Habbab Sep 20 '21 at 10:06
  • 2
    If you want the date portion to default to today's date, then [`mktime`](https://www.php.net/manual/en/function.mktime.php) might be what you're looking for, just pass a zero for hour + second arguments. But your question isn't totally clear. – iainn Sep 20 '21 at 10:10
  • 1
    Does this answer your question? [Adding minutes to date time in PHP](https://stackoverflow.com/questions/8169139/adding-minutes-to-date-time-in-php) – Hendrik Sep 20 '21 at 10:24
  • sorry I sould have added more infos, i have a column that includes the dates for every record I will use those dates to fill the date part in the convertion – Dr.Noob Sep 20 '21 at 10:38
  • 1
    This is still a bit ambiguous, as you are talking about columns. Do you by any chance might want to use SQL for this instead? As in you have timestamp in one column, minutes in another, so maybe you could just do something like this: `UPDATE my_table SET duration = duration + duration_temp*60;` – Hendrik Sep 20 '21 at 11:20

4 Answers4

1

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();
Rateb Habbab
  • 1,739
  • 8
  • 13
1

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

Hendrik
  • 756
  • 6
  • 16
1

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
Atika
  • 1,025
  • 2
  • 6
  • 17
  • Depending on the local settings of the server (time zone), a different result is provided with this code. – jspit Sep 20 '21 at 16:46
0

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.

jspit
  • 7,276
  • 1
  • 9
  • 17