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:109
Avg. Score:3.4 ± 1.1
Reproduced:47 of 55 (85.5%)
Same Version:25 (53.2%)
Same OS:19 (40.4%)
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
 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Sat Jan 11 00:01:29 2025 UTC