Req #75623 [NEW]: DateTime::createFromTimestamp() method
| From: | saifmsg at yahoo dot com | Date: | Mon, 04 Dec 2017 16:42:38 +0000 |
| Subject: | Req #75623 [NEW]: DateTime::createFromTimestamp() method | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-212910@lists.php.net to get a copy of this message | ||
From: saifmsg at yahoo dot com
Operating system: Linux
PHP version: 7.1.12
Package: Date/time related
Bug Type: Feature/Change Request
Bug description:DateTime::createFromTimestamp() method
Description:
------------
There need to be an easy way to create DateTime object from unix
timestamp when "declare(strict_types=1)" is set. The problem is
"createFromFormat" expects second parameter to be a string and it forces
it to be casted into string.
There are other ways also but not quite simple (see the attached code).
It would be simple and easy if there is a factory method:
DateTime::createFromTimestamp(int $timestamp)
This was requested before the release of "strict_types", original
feature request: https://bugs.php.net/bug.php?id=43595
Test script:
---------------
<?php
declare(strict_types=1);
// get timestmap as int from model
// for test lets set the current time.
$timestamp = time();
// Option: 1
$dateTime = DateTime::createFromFormat('U', (string)$timestamp);
// Option: 2a
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);
// Option: 2b
$dateTime = (new DateTime())->setTimestamp($timestamp);
// While it could have been simpler:
// $dateTime = DateTime::createFromTimestamp($timestamp);
--
Edit bug report at https://bugs.php.net/bug.php?id=75623&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75623&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75623&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75623&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75623&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75623&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75623&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75623&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75623&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75623&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75623&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75623&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75623&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75623&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75623&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75623&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75623&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75623&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75623&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75623&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75623&r=mysqlcfg