Jump to content

L55 Carbon, display time until next time etc (11:00)

Joveice

Hey, how can I make Carbon in laravel 5.5 show the time until next etc 15:00 or 18:30? As it's now it shows until next, but if it's over the day it will go in minus.

 

 

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

I have no idea about Carbon in laravel but from a pure PHP perspective I would.

 

$date = new DateTime('2000-01-01 9:00:00'); //your next time/date - make sure it's in the future
//$date is equal to Jan 1st 2000 9am

$ts1 = $date->getTimestamp();

 

$res = $ts1 - time(); //this will give you seconds until the time

 

I'm sure you can figure out the rest.

i want to die

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, Lumi said:

I have no idea about Carbon in laravel but from a pure PHP perspective I would.

 


$date = new DateTime('2000-01-01 9:00:00'); //your next time/date - make sure it's in the future
//$date is equal to Jan 1st 2000 9am

$ts1 = $date->getTimestamp();

 

$res = $ts1 - time(); //this will give you seconds until the time

 

I'm sure you can figure out the rest.

Yea, I can do this in laravel with one thing, but that does not find the next date if it has passed, and that's currently the issue,

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

So the Carbon class is inherited from the DateTime class...

So this is kind of simple:

<?php
	/* Dates */
    $now = Carbon::now();
    $tomorrow = Carbon::tomorrow()->setTime(15,0,0);

    /* DateTime::diff returns a DateInterval object */
    $differenceInDaysHoursMinutesSeconds = $now->diff($tomorrow)->format('%a days, %h hours, %i minutes and %s seconds');

    /* If you only need the hours or minutes, Carbon comes with some handy methods: */
    $differenceInDays  = $now->diffInDays($tomorrow);
    $differenceInHours = $now->diffInHours($tomorrow);
    
    /* ... Check the Carbon documentation for more info */

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

I think I got your problem, you want to get the next 18:30 and if not the next 19:30 ... and so on?

 

I would extend the Carbon Class to do so, I use the laravel collection, but can easily be done without it ;) 

<?php

class MyCarbon extends Carbon{

    /**
     * Returns next possible time
     * 
     * @param array $times
     * @return Carbon
     */
    public function nextTime(array $times): Carbon{

        /* Sort the Collection */
        $timeCollection = collect($times)->sortBy(0);

        /* Get last Time */
        $lastTime = $this->copy()->setTimeByArray($timeCollection->last());

        /* Add one Day if the last Time is passed */
        if($lastTime->isPast())
            $this->addDay();

        /* Loop through times */
        $timeCollection->each(function($time){

            /* Modify and break the loop if the time is in future */
            if( $this->setTimeByArray($time)->isFuture() )
                return false;
        });

        return $this;
    }

    /**
     * Set the time by array
     * 
     * @param array $time
     * @return Carbon
     */
    public function setTimeByArray(array $time):Carbon{

        /* Set the Time by Array */
        return $this->setTime(
            isset($time[0])? $time[0]:0,
            isset($time[1])? $time[1]:0,
            isset($time[2])? $time[2]:0,
            isset($time[3])? $time[3]:0
        );

    }

}

Now you can easily get the next time

<?php

    /* will return the next available time */
    $next = MyCarbon::now()->nextTime([
      [ 1,30],
      [ 3],
      [16,30],
      [17,30],
      [18,30],
    ]);

 

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, leodaniel said:

I think I got your problem, you want to get the next 18:30 and if not the next 19:30 ... and so on?

 

I would extend the Carbon Class to do so, I use the laravel collection, but can easily be done without it ;) 


<?php

class MyCarbon extends Carbon{

    /**
     * Returns next possible time
     * 
     * @param array $times
     * @return Carbon
     */
    public function nextTime(array $times): Carbon{

        /* Sort the Collection */
        $timeCollection = collect($times)->sortBy(0);

        /* Get last Time */
        $lastTime = $this->copy()->setTimeByArray($timeCollection->last());

        /* Add one Day if the last Time is passed */
        if($lastTime->isPast())
            $this->addDay();

        /* Loop through times */
        $timeCollection->each(function($time){

            /* Modify and break the loop if the time is in future */
            if( $this->setTimeByArray($time)->isFuture() )
                return false;
        });

        return $this;
    }

    /**
     * Set the time by array
     * 
     * @param array $time
     * @return Carbon
     */
    public function setTimeByArray(array $time):Carbon{

        /* Set the Time by Array */
        return $this->setTime(
            isset($time[0])? $time[0]:0,
            isset($time[1])? $time[1]:0,
            isset($time[2])? $time[2]:0,
            isset($time[3])? $time[3]:0
        );

    }

}

Now you can easily get the next time


<?php

    /* will return the next available time */
    $next = MyCarbon::now()->nextTime([
      [ 1,30],
      [ 3],
      [16,30],
      [17,30],
      [18,30],
    ]);

 

 

Actually, I think what I want is the next time 18:30, and if it has passed it on today's date, I need it to get 18:30 of next day. I forgot that I get time left using a frontend script, so what it needs is the date with time.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

37 minutes ago, Joveice said:

Actually, I think what I want is the next time 18:30, and if it has passed it on today's date, I need it to get 18:30 of next day. I forgot that I get time left using a frontend script, so what it needs is the date with time.

Thats simple, my solution above works with multiple times, if lets say you want to get the next possible time with multiple times ;) 

<?php

    $date = Carbon::now()->setTime(18,30,0);

    if($date->isPast())
      $date->addDay();

    

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, Joveice said:

Yea, I can do this in laravel with one thing, but that does not find the next date if it has passed, and that's currently the issue,

Yeah but like,

if ($ts1 < time()) //recursion

i want to die

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×