發表文章

目前顯示的是 12月, 2015的文章

PHP 產生指定長度的亂數字串,數字

function randomStr($length) { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $result = ''; for ($i = 0; $i < $length; $i++) { $result .= $chars[ mt_rand(0, strlen($chars) - 1) ]; } return $result; } function randomNum($length) { $result = ''; for ($i = 0; $i < $length; $i++) { $result .= mt_rand(0, 9); } return $result; }

PHP 把頁面引導到404 file not found

header("HTTP/1.0 404 Not Found"); exit;

PHP 日期加減

$timeString = '2015-12-16'; // Y-m-d G:i:s; // 加5年 echo date('Y-m-d G:i:s', strtotime($timeString."+5 year")); // 2020-12-16 0:00:00 // 加7個月 echo date('Y-m-d G:i:s', strtotime($timeString."+7 month")); // 2016-07-16 0:00:00 // 減1天 echo date('Y-m-d G:i:s', strtotime($timeString."-1 day")); // 2015-12-15 0:00:00 // 減3週 echo date('Y-m-d G:i:s', strtotime($timeString."-3 week")); // 2015-11-25 0:00:00 // 加5小時 echo date('Y-m-d G:i:s', strtotime($timeString."+5 hour")); // 2015-12-16 5:00:00 // 加7分鐘 echo date('Y-m-d G:i:s', strtotime($timeString."+7 minute")); // 2015-12-16 0:07:00 // 減3秒 echo date('Y-m-d G:i:s', strtotime($timeString."-3 seconds")); // 2015-12-15 23:59:57

PHP 判所現在時間是否在某個區間內

// 如果現在時間介於2015年12月24日早上10點 到 2015年12月25日晚上10點之間的話 if( time() > strtotime("2015-12-24 10:00:00") and time() < strtotime("2015-12-25 22:00:00") ) { // do something .. }

PHP 日期轉換

// 把字串格式的時間轉成php的時間 (秒數); strtotime('Y-m-d G:i:s'); // 1450297476 echo strtotime('2015-12-16 12:24:36'); // 把php時間(秒數)轉成指定字串格式 date('Y-m-d G:i:s', time()); // 2015-12-16 12:24:36 echo date('Y-m-d G:i:s', 1450297476);