Skip navigation

Monthly Archives: Mei 2016


Just met a problem when I tried to serialize a PHP DateInterval object in HHVM (at least for 3.9.1). Turns out the DateInterval is unserializable. Create a bit workaround for this.

<?php

$dateinterval1 = new DateInterval('PT1H');
$baseDate = new DateTimeImmutable();
$date1 = $baseDate->add($dateinterval1);
$string1 = $dateinterval1->format('%y years %m months %d days %h hours %i minutes %s seconds');

$dateinterval2 = DateInterval::createFromDateString($string1);
$date2 = $baseDate->add($dateinterval2);

$string2 = $dateinterval2->format('%y years %m months %d days %h hours %i minutes %s seconds');
if ($string1 == $string2) {
    print "Formats are equal\n";
}
if ($date1 == $date2) {
    print "Objects are equal\n";
}
if ($date1->getTimestamp() == $date2->getTimestamp()) {
    print "Timestamp are equal\n";
}

The test can be seen here: https://3v4l.org/HvLFP

%d blogger menyukai ini: