ramiromd
ramiromd

Reputation: 2029

Incorrect date at print

im try to print the users birthday of my app, the dates are allowed in a database. In the DB have the user #37 with birthday at '1900-01-01'.

The User model defines, birthday, accessors and property as follow:

/**
 * @ORM\Column(type="date")
 */
 protected $fechaDeNacimiento;

/**
 * @param \DateTime $fechaDeNacimiento
 * @return User
 */
 public function setFechaDeNacimiento($fechaDeNacimiento)
 {
   $this->fechaDeNacimiento = $fechaDeNacimiento;

   return $this;
 }

 /**
  * @return \DateTime 
  */
  public function getFechaDeNacimiento()
  {
    return $this->fechaDeNacimiento;
  }

With Twig i write:

{{ user.fechaDeNacimiento | date("m-d-Y")}}

But the result is not '01-01-1900', is '12-31-1899'. The format is correct, but the date not.

Any ideas ?.

Upvotes: 0

Views: 96

Answers (1)

Mick
Mick

Reputation: 31919

Leap Second

This is due to leap second.

A leap second is a one-second adjustment that is occasionally applied to Coordinated Universal Time (UTC) in order to keep its time of day close to the mean solar time.

Specifically, a positive leap second is inserted between second 23:59:59 of a chosen UTC calendar date (the last day of a month, usually June 30 or December 31) and second 00:00:00 of the following date.

And indeed, a leap second has occurred in 12-31-1899 as referenced here.


Edit

Most databases support Time Zone Leap Second, for example MySQL

To give you an idea, here is what happens for the leap second defined in 2008-12-31

That's a perfect example in 2008:

mysql> CREATE TABLE t1 (
    ->   a INT,
    ->   ts TIMESTAMP DEFAULT NOW(),
    ->   PRIMARY KEY (ts)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> -- change to UTC
mysql> SET time_zone = '+00:00';
Query OK, 0 rows affected (0.00 sec)

mysql> -- Simulate NOW() = '2008-12-31 23:59:59'
mysql> SET timestamp = 1230767999;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO t1 (a) VALUES (1);
Query OK, 1 row affected (0.00 sec)

mysql> -- Simulate NOW() = '2008-12-31 23:59:60'
mysql> SET timestamp = 1230768000;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO t1 (a) VALUES (2);
Query OK, 1 row affected (0.00 sec)

mysql> -- values differ internally but display the same
mysql> SELECT a, ts, UNIX_TIMESTAMP(ts) FROM t1;
+------+---------------------+--------------------+
| a    | ts                  | UNIX_TIMESTAMP(ts) |
+------+---------------------+--------------------+
|    1 | 2008-12-31 23:59:59 |         1230767999 |
|    2 | 2008-12-31 23:59:59 |         1230768000 |
+------+---------------------+--------------------+
2 rows in set (0.00 sec)

mysql> -- only the non-leap value matches
mysql> SELECT * FROM t1 WHERE ts = '2008-12-31 23:59:59';
+------+---------------------+
| a    | ts                  |
+------+---------------------+
|    1 | 2008-12-31 23:59:59 |
+------+---------------------+
1 row in set (0.00 sec)

Upvotes: 1

Related Questions