I'd like to represent time (standalone) in PHP without the date part.
I've tried using strtotime and DateTime::createFromFormat but both add a date part to it. Laravel's carbon library descends from DateTime and also doesn't cater for this.
My use case is that of representing bus depart and arriving times from a station. The buses depart everyday at the same time so I don't want/need the date part of it.
Python has datetime.time and Java has java.time.LocalTime, is there a direct equivalent of this in PHP? Should I create a custom class for it?
Cheers
Create DateTime object and format this object to show only time:
$date = new \DateTime("now");
echo $date->format("H:i:s");
or do the same thing in one line:
echo (new \DateTime("now"))->format("H:i:s");
Use date function:
echo date("H:i:s");
PHP doesn't have a direct equivalent to either Python datetime.time nor Java  java.time.LocalTime. A Time value-object should be created for this case in a per-need basis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With