PHP code example of arokettu / datetime-rounding

1. Go to this page and download the library: Download arokettu/datetime-rounding library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arokettu / datetime-rounding example snippets




use Arokettu\DateTime\DateTimeTruncate;
use Carbon\CarbonImmutable;

// to hours
$dateTime = new DateTimeImmutable('2012-03-04T05:06:07.890123Z');
echo DateTimeTruncate::toHours($dateTime)->format('c'); // 2012-03-04T05:00:00+00:00

// truncating to dates uses the DT timezone
$dateTime = new DateTimeImmutable('2012-03-04T05:06:07.890123 Europe/Stockholm');
echo DateTimeTruncate::toMonths($dateTime)->format('c'); // 2012-03-01T00:00:00+01:00

// using mutable will change the object
$dateTime = new DateTime('2012-03-04T05:06:07.890123Z');
DateTimeTruncate::toMinutes($dateTime);
echo $dateTime->format('c'); // 2012-03-04T05:06:00+00:00

// the tool tries to preserve the extended objects as well
$dateTime = new CarbonImmutable('2012-03-04T05:06:07.890123Z');
echo get_class(DateTimeTruncate::toMonths($dateTime)); // Carbon\CarbonImmutable