Date and time

It is possible to manipulate dates and times using the Convertime class. To begin, run the instance:

use Solital\Core\Resource\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");

Format date

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);

Add months to a date

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);

Add days to a date

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);

Add time to another time

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);

What to see next?


Built with MkDocs.