需要具有 php 的 vbscript 命令 cdbl(now) 的等效解决方案
Need a equivalent solution for vbscript command cdbl(now) with php
使用 vbscript,我可以使用命令 cdbl(now)
.
从当前 date/time 中检索双精度值
Now = '09.05.2015 21:44:10'
cdbl(Now) = 42133,9056712963
我正在寻找 php 的等效解决方案,它将返回当前 date/time 的双倍值。
cdbl return 微软格式的日期时间。逗号日期之前 - 自 1900 年 1 月 1 日以来的天数。逗号时间之后 - 24 小时为 1。因此,如果您使用 Unix 纪元内的日期(代码可能无效,我想让它更容易理解)
function cdbl($str) {
$datetime0 = new DateTime('1970-01-01');
$datetime = new DateTime($str);
// 25560 - days from 1 Jan 1900 to 1 Jan 1970
$cdbl = $datetime->diff($datetime0)->days + 25569;
// Remove time from string - it is the sane as 00:00
$str0 = preg_replace('/((T|\s+).+)$/','', $str);
// The number of seconds since the day start
$time = strtotime($str) - strtotime($str0);
// The number of seconds wittin a day
$timefullday = strtotime("$str0 + 1 day") - strtotime($str0);
$cdbl += $time / $timefullday;
return $cdbl;
}
echo cdbl('09.05.2015 21:44:10');
结果:
42133.905671296
如您所见,存在四舍五入的问题。如果在求和之前打印计算时间的结果,答案将与您的答案相同。我从来没有做过计算任务,所以我不能告诉你用它做什么。唯一的建议是转换为字符串 :)
好吧,如果您选择在 Unix 日期之外工作,您应该编写一些代码
以下函数returns当前 Unix 时间戳作为双精度值:
function cdblnow(){
// time in seconds since 1 Jan 1970 (GMT)
$timeunix = time();
// add the timezone offset
$TimeZone = "Asia/Bangkok";
$dateTimeZone = new DateTimeZone($TimeZone);
$dateTime = new DateTime("now", $dateTimeZone);
$timeOffset = $dateTimeZone->getOffset($dateTime);
$timeunix = $timeunix + $timeOffset;
// determines the days between 1 Jan 1970 and today
$days = intval($timeunix / 86400);
// second count from today
$secondsremains = $timeunix % 86400;
// 25569 days difference between microsoft and unix time stamp start
$now_date = $days + 25569;
// 0.0000115741 represents one second at the cdbl-function from microsoft
$now_time = $secondsremains * 0.0000115741;
return $now_date + $now_time;}
此函数returns 来自双精度的 unix 时间戳:
function GetUnixTimeFromCdblNow($cdbl){
$days = intval($cdbl);
$seconds = round($cdbl - $days,9);
$timeunix = (($days -25569) * 86400);
$timeunix = $timeunix + intval($seconds / 0.0000115740);
return $timeunix;}
使用 vbscript,我可以使用命令 cdbl(now)
.
Now = '09.05.2015 21:44:10'
cdbl(Now) = 42133,9056712963
我正在寻找 php 的等效解决方案,它将返回当前 date/time 的双倍值。
cdbl return 微软格式的日期时间。逗号日期之前 - 自 1900 年 1 月 1 日以来的天数。逗号时间之后 - 24 小时为 1。因此,如果您使用 Unix 纪元内的日期(代码可能无效,我想让它更容易理解)
function cdbl($str) {
$datetime0 = new DateTime('1970-01-01');
$datetime = new DateTime($str);
// 25560 - days from 1 Jan 1900 to 1 Jan 1970
$cdbl = $datetime->diff($datetime0)->days + 25569;
// Remove time from string - it is the sane as 00:00
$str0 = preg_replace('/((T|\s+).+)$/','', $str);
// The number of seconds since the day start
$time = strtotime($str) - strtotime($str0);
// The number of seconds wittin a day
$timefullday = strtotime("$str0 + 1 day") - strtotime($str0);
$cdbl += $time / $timefullday;
return $cdbl;
}
echo cdbl('09.05.2015 21:44:10');
结果:
42133.905671296
如您所见,存在四舍五入的问题。如果在求和之前打印计算时间的结果,答案将与您的答案相同。我从来没有做过计算任务,所以我不能告诉你用它做什么。唯一的建议是转换为字符串 :)
好吧,如果您选择在 Unix 日期之外工作,您应该编写一些代码
以下函数returns当前 Unix 时间戳作为双精度值:
function cdblnow(){
// time in seconds since 1 Jan 1970 (GMT)
$timeunix = time();
// add the timezone offset
$TimeZone = "Asia/Bangkok";
$dateTimeZone = new DateTimeZone($TimeZone);
$dateTime = new DateTime("now", $dateTimeZone);
$timeOffset = $dateTimeZone->getOffset($dateTime);
$timeunix = $timeunix + $timeOffset;
// determines the days between 1 Jan 1970 and today
$days = intval($timeunix / 86400);
// second count from today
$secondsremains = $timeunix % 86400;
// 25569 days difference between microsoft and unix time stamp start
$now_date = $days + 25569;
// 0.0000115741 represents one second at the cdbl-function from microsoft
$now_time = $secondsremains * 0.0000115741;
return $now_date + $now_time;}
此函数returns 来自双精度的 unix 时间戳:
function GetUnixTimeFromCdblNow($cdbl){
$days = intval($cdbl);
$seconds = round($cdbl - $days,9);
$timeunix = (($days -25569) * 86400);
$timeunix = $timeunix + intval($seconds / 0.0000115740);
return $timeunix;}