PHP Carbon take number of minutes & convert to days
Asked Answered
T

4

5

I am trying to convert a number on minutes into number of days using Carbon.

$minutes = 1400;

I want to do something like below (which of course, does not work):

Carbon->minutes($minutes)->days();

I cannot find an example of this. Thanks for any help.

Thistle answered 18/4, 2016 at 3:24 Comment(0)
J
8

Not tested, but a quick look at the docs suggests it might be aimed more at DateTime objects than differences in time, so you could do something like this:

$now  = Carbon::now();
$days = $now->diffInDays($now->copy()->addMinutes($minutes));
Joiner answered 18/4, 2016 at 3:33 Comment(0)
C
4

Maybe CarbonInterval::minutes($minutes)->cascade();?

Will cascade down your value and format it for humans.

Caphaitien answered 23/3, 2019 at 11:38 Comment(0)
B
2

If you want to convert seconds to Jira style 1y 1m 1w 1h 1m 1s:

class Translator implements TranslatorInterface, TranslatorBagInterface
{
    protected const TRANS = [
        'year'   => 'y',
        'month'  => 'm',
        'week'   => 'w',
        'day'    => 'd',
        'hour'   => 'h',
        'minute' => 'm',
        'second' => 's',
    ];

    public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
    {
        return $parameters[':count'].self::TRANS[$id];
    }

    public function getCatalogue(string $locale = null)
    {
        return new \Symfony\Component\Translation\MessageCatalogue('pl_PL');
    }
}
$second = 1;
$minute = 60 * $second;
$hours  = 60 * $minute;
$day    = 24 * $hours;
$week   = 7  * $day;
$month  = 4  * $week;
$year   = 12 * $month;

$sum      = $second + $minute + $hours + $day + $week + $month + $year;
$interval = CarbonInterval::seconds($sum)->cascade();

$interval->setLocalTranslator(new Translator());
echo $interval->forHumans(); // 1y 1m 1w 1h 1m 1s
Bifocals answered 11/1, 2022 at 12:55 Comment(0)
P
1

if you want to convert to years, months, weeks, days, hours, minutes, seconds, microseconds

use Carbon\CarbonInterval;
echo CarbonInterval::minutes(2212)->cascade()->forHumans();

//output  = 1 day 12 hours 52 minutes

Protanopia answered 1/8, 2021 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.