php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40171 There should be a way to create DateTime object from UTC timestamp.
Submitted: 2007-01-19 16:27 UTC Modified: 2007-01-19 18:25 UTC
Votes:101
Avg. Score:3.4 ± 1.1
Reproduced:43 of 51 (84.3%)
Same Version:21 (48.8%)
Same OS:16 (37.2%)
From: rkumar at msgme dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.2.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rkumar at msgme dot com
New email:
PHP Version: OS:

 

 [2007-01-19 16:27 UTC] rkumar at msgme dot com
Description:
------------
The constructor for DateTime object currently takes a string 
as a parameter. In most of the scenarios that I have encountered I need to create a DateTime object from UTC timestamp. Can the constructor  be changed to accept string or integer? 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-19 18:25 UTC] derick@php.net
That would be too confusing, as PHP auto casts and "20070119" is something quite different from the unix timestamp 20070119. What you can do is pass this to the ctor: "@20070119" - the @ makes it interpret as a Unix timestamp.
 [2020-07-08 02:32 UTC] vin526vin at gmail dot com
<?php
if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD'])  || isset($_SERVER['REMOTE_ADDR'])) {
        die("<br><strong>This script is only meant to run at the command line.</strong>");
}
error_reporting(E_ERROR);

$args = getopt("v:c:h:");
$strResult = exec( "/bin/snmpwalk -v ".$args['v']." -c ".$args['c']." ".$args['h']." .1.3.6.1.2.1.25.1.2");
$aryDate = explode("," , str_replace('STRING: ', '', substr($strResult, strpos($strResult, 'STRING'))));
$strDate = $aryDate[0].' '.substr($aryDate[1], 0, -2);

$d1 = new DateTime($strDate);
$d2 = new DateTime(date("Y-m-d H:i:s"));
$diff=date_diff($d1,$d2);
$totalDiff = $diff->format("%d")*86400+$diff->format("%h")*3600+$diff->format("%i")*60+$diff->format("%s");

echo "TimeDiff:".$totalDiff."\n";
?>
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri May 14 10:01:23 2021 UTC