Search code examples
phpphp-carbon

PHP Carbon - Create a date in future relative to current time


Imagine we have the day (of week) and the time of an event ( that is in the future ) :

$day = "Wednesday";
$time = "13:00";

And we want to create a Carbon date for that, but it has to be relative to current time :

$carbon = new Carbon();

For example if $carbon is Tuesday, 2017-01-17 09:00, the $event date that we create should be Wednesday, 2017-01-18 13:00

In other words, the $event is a date that is taking place in the next 7 days. We don't have it's year, month and other data, but we do know it's day of week and the time.


Solution

  • you should be able to use something like:

    Carbon::parse('next ' . $day)
    

    and you can also throw the $time part in there somewhere too but can't remember exactly how right now.