Home > php教程 > php手册 > body text

php日期转时间戳,指定日期转换成时间戳

PHPz
Release: 2018-10-09 15:57:20
forward
1130 people have browsed it

UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储、处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出PHP日期转时间戳、MySQL日期转换函数互相转换的几种转换方式

写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储、处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出互相转换的几种转换方式。

一、在MySQL中完成

这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性。
1. UNIX时间戳转换为日期用函数: FROM_UNIXTIME()
一般形式:select FROM_UNIXTIME(1156219870);
2. 日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP()
一般形式:Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);
举例:mysql查询当天的记录数:
$sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') order by id desc”;
当然大家也可以选择在PHP中进行转换,下面说说在PHP中转换。

二、在PHP中完成

这种方式在PHP程序中完成转换,优点是无论是不是数据库中查询获得的数据都能转换,转换范围不受限制,缺点是占用PHP解析器的解析时间,速度相对慢。
1. UNIX时间戳转换为日期用函数: date()
一般形式:date('Y-m-d H:i:s', 1156219870);
2. 日期转换为UNIX时间戳用函数:strtotime()
一般形式:strtotime('2010-03-24 08:15:42');

php日期转时间戳,指定日期转换成时间戳

php日期转时间戳、指定日期转换成时间戳,PHP定时任务。
这两天要实现这样功能:
当达到某一条件时,让服务器发短信给用户,数量为多条。
基本思路:linux 定时扫描,若有满足条件的用户,则发送短信。
但为了防止打扰到用户,要求只能在白天8:00-20:00发送短信,怎么样获得到每天的这段时间区间?
如下代码:

代码如下:

<? 
$y=date("Y",time()); 
$m=date("m",time()); 
$d=date("d",time()); 
$start_time = mktime(9, 0, 0, $m, $d ,$y); 
$end_time = mktime(19, 0, 0, $m, $d ,$y); 
$time = time(); 
if($time >= $start_time && $time <= $end_time) 
{ 
// do something.... 
} 
?>
Copy after login

更多相关教程请访问 php编程从入门到精通全套视频教程

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!