Home  >  Article  >  Backend Development  >  Convert between PHP timestamp and date_PHP Tutorial

Convert between PHP timestamp and date_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:13928browse

In PHP, if we want to convert a date into a timestamp, we can directly use the strtotime function. If we want to convert a timestamp into a date, we can directly use the date() function. Let me introduce it to my friends.

strtotime() function The strtotime() function parses the date-time description of any English text into a Unix timestamp.


Example

The code is as follows
 代码如下 复制代码

echo(strtotime("now"));
echo(strtotime("3 October 2005"));
echo(strtotime("+5 hours"));
echo(strtotime("+1 week"));
echo(strtotime("+1 week 3 days 7 hours 5 seconds"));
echo(strtotime("next Monday"));
echo(strtotime("last Sunday"));
?>

输出:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200

Copy code

echo(strtotime("now"));
代码如下 复制代码

$a = date();
$mk = strtotime($a)

echo(strtotime("3 October 2005"));

echo(strtotime("+5 hours"));

echo(strtotime("+1 week"));

代码如下 复制代码


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

echo(strtotime("+1 week 3 days 7 hours 5 seconds"));

echo(strtotime("next Monday"));
echo(strtotime("last Sunday")); ?>

Output:


1138614504

1128290400

1138632504

1139219304
 代码如下 复制代码

$time = time();
$date = date("Y-m-d",$time);
echo 'www.bKjia.c0m 提示'.$date
?>

1139503709

1139180400

1138489200

The above is to convert the

date into a timestamp
The code is as follows Copy code
$a = date(); $mk = strtotime($a) It is required that text messages can only be sent between 8:00-20:00 during the day. How can I get this time range every day?
The code is as follows Copy code
$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.... <🎜> } <🎜> ?> Next, we will introduce how to convert time timestamp into date date() function, This function can not only obtain various times and dates, but also perform date conversion
The code is as follows Copy code
$time = time();<🎜> $date = date("Y-m-d",$time);<🎜> echo 'www.bKjia.c0m prompt'.$date<🎜> ?> This will output www.bKjia.c0m prompt 2013-04-21. http://www.bkjia.com/PHPjc/628855.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628855.htmlTechArticleIn php, if we want to convert date into timestamp, we can directly use the strtotime function. If we convert the timestamp into The date can be implemented directly using the date() function. Let me give you...
Statement:
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