接口需要添加一個定時發布的功能,這樣可以提前準備好內容,時間一到,用戶就能看到。這裡用到js和php的日期操作。
大概搜了下html5中用datetime相關的表單,但是由於瀏覽器實現的問題,為了穩定性最終放棄。使用"YYYY-MM-DD HH:mm:ss"這樣的文本來保存設置時間,後台轉換成utc時間戳,最後通過對比時間戳來決定是否發布。時間全部顯示為北京時間。php使用了thinkphp框架。js和php默認返回的時間戳都是UTC時間。
後台的添加新聞模板:
<input type="text" class="" size="100" maxlength="300" name="publishTimeU" id="publishTimeU" value=""/> <input type="hidden" maxlength="100" name="publishTime" id="publishTime" value=""/> <script type="text/javascript"> (function($){ var date = new Date(); date = date.toISOString().substr(0, 19)+"-08:00"; var date1 = new Date(date); date1 = date1.toISOString().substr(0, 19); date1 = date1.replace(/T/, " "); $("#publishTimeU").val(date1); //定時發布失去焦點時,換算時間戳 $("#publishTimeU").blur(function(){ var ptString=$("#publishTimeU").val(); ptString=ptString.replace(/ /, "T") var pt=Date.parse(ptString+"+08:00")/1000; $("#publishTime").val(pt); }); })(jQuery); </script>
後台的修改新聞模板:
<input type="text" class="" size="100" maxlength="300" name="publishTimeU" id="publishTimeU" value=""/> <input type="hidden" maxlength="100" name="publishTime" id="publishTime" value="{$vo.publishTime}"/> <script type="text/javascript"> (function($){ var date = new Date($("#publishTime").val()*1000); date = date.toISOString().substr(0, 19)+"-08:00"; var date1 = new Date(date); date1 = date1.toISOString().substr(0, 19); date1 = date1.replace(/T/, " "); $("#publishTimeU").val(date1); //定時發布失去焦點時,換算時間戳 $("#publishTimeU").blur(function(){ var ptString=$("#publishTimeU").val(); ptString=ptString.replace(/ /, "T") var pt=Date.parse(ptString+"+08:00")/1000; $("#publishTime").val(pt); }); })(jQuery); </script>
php接口:
$now = new DateTime(null, new DateTimeZone('Asia/Hong_Kong')); $nowTimestamp = $now->getTimestamp(); //只需在查詢語句的map里添加時間戳對比即可 $map = array ( 'h' => array('like','%'.$cid .'%'), 'publishTime' => array('LT',$nowTimestamp), 'status' => 1 ); $count = $model->where ( $map )->cache (false)->count ();