It is possible to manipulate dates and times using the Convertime
class. To begin, run the instance:
use Solital\Core\Validation\Convertime;
$convertime = new Convertime();
In the class constructor, you can define the timezone. By default, the default
timezone is set to America/Sao_Paulo
.
$convertime = new Convertime("America/Fortaleza");
You can change the timezone also in the bootstrap.yaml
file:
default_timezone: America/Fortaleza
To format a date, enter the date you want to convert and the format to be converted.
$convertime = new Convertime();
$res = $convertime->formatDate('04/01/1999', 'Y-m-d');
/* Return 1999-01-04 */
pre($res);
In some cases, you may need to add months to a specific date. To do this, use the addMonth
class.
This class is similar to the formatDate
class, with the difference that you must enter
in the last parameter the number of months that will be added to the date.
This class already has conversion for days with 28, 29, 30 or 31 days.
$convertime = new Convertime();
$res = $convertime->addMonth('1999-01-04', 'd/m/Y', 1);
/* Return 1999-02-04 */
pre($res);
To add days to a date, the addDays
method works in a similar way to theaddMonth
method.
$convertime = new Convertime();
$res = $convertime->addDays('1999-01-04', 'd/m/Y', 3);
/* Return 1999-01-07 */
pre($res);
It is possible to add a specific time to another time. For example: add 3 more hours at 13:00.
$convertime = new Convertime();
$res = $convertime->addHour('13:00', '03:00');
/* Return 16:00 */
pre($res);
To check whether a certain date will be on a weekend, use the isWeekend()
method.
$convertime = new Convertime();
$res = $convertime->isWeekend('2021-07-18');
/* Return bool */
pre($res);