Skip navigation


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

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: