简体   繁体   English

使用日期和StrToTime

[英]Using Date & StrToTime

I've been using the following bit of code without any issues till it finally landed on a windows server and decided to produce an error: 我一直在使用以下代码,没有任何问题,直到它最终登陆Windows服务器并决定产生错误为止:

$date = date('F d, Y', $data->e_date)
date($time_format, strtotime($date.' '.$data->e_time))

*(e_date is stored like this: "1293559200", and e_time is stored like this: "18:00")* *(e_date这样存储:“ 1293559200”,e_time这样存储:“ 18:00”)*

The error is as such: 错误是这样的:

date() [function.date]: Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in ...

To my understanding this is because I am using strtotime within the date function . 据我了解,这是因为我在date函数中使用了strtotime So what I'm wondering is what is an elegant method of solving or rewriting this? 所以我想知道什么是解决或重写此问题的理想方法?

Should I be feeding the entire strtotime in a new variable, ie $newdate and then back to date in that form, or otherwise? 我是否应该将整个strtotime放入新变量(即$ newdate)中,然后以该形式返回最新日期,否则?

Thank you! 谢谢!

You are using UNIX TIMESTAMP it will not work properly in windows os systems. 您正在使用UNIX TIMESTAMP,它将无法在Windows OS系统中正常工作。

Try to convert it using this function. 尝试使用此功能将其转换。 convert 20031230233029 to 30/12/2003 23:30:59 转换20031230233029为30/12/2003 23:30:59

function mysql_timestamp_para_humano($dt) {
    $yr=strval(substr($dt,0,4));
    $mo=strval(substr($dt,4,2));
    $da=strval(substr($dt,6,2));
    $hr=strval(substr($dt,8,2));
    $mi=strval(substr($dt,10,2));
    $se=strval(substr($dt,12,2)); 
    return date("d/m/Y H:i:s", mktime ($hr,$mi,$se,$mo,$da,$yr));
}

or this one 或这个

function timestamp_para_humano($ts) {
    $d=getdate($ts);
    $yr=$d["year"];
    $mo=$d["mon"];
    $da=$d["mday"];
    $hr=$d["hours"];
    $mi=$d["minutes"];
    $se=$d["seconds"];
    return date("d/m/Y", mktime($hr,$mi,$se,$mo,$da,$yr));
}

or you can try to convert the unix timestamp to mysql timestamp wth this another function 或者您可以尝试通过另一个函数将unix时间戳转换为mysql时间戳

function timestamp_para_mysql_timestamp($ts) {
    $d=getdate($ts);
    $yr=$d["year"];
    $mo=$d["mon"];
    $da=$d["mday"];
    $hr=$d["hours"];
    $mi=$d["minutes"];
    $se=$d["seconds"];
    return sprintf("%04d%02d%02d%02d%02d%02d",$yr,$mo,$da,$hr,$mi,$se);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM