简体   繁体   中英

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:

$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")*

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 . 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?

Thank you!

You are using UNIX TIMESTAMP it will not work properly in windows os systems.

Try to convert it using this function. convert 20031230233029 to 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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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