如何将日期和时间转换为php中的时间戳? [英] how to convert date and time to timestamp in php?

查看:147
本文介绍了如何将日期和时间转换为php中的时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期 '07 / 23/2009'和一个时间 '18:11'和我想要得到一个时间戳:
这是我的例子:

i have a date '07/23/2009' and a time '18:11' and i want to get a timestamp out of it : here is my example:

date_default_timezone_set('UTC');

$d = str_replace('/', ', ', '07/23/2009');
$t = str_replace(':', ', ', '18:11');
$date = $t.', 0, '.$d;
echo $date;
echo '<br>';
echo $x = mktime("$date");

问题是 $ x 给我目前的时间戳。

the issue is that $x gives me the current timestamp.

任何想法?

推荐答案

因为mktime函数仅需要数字的所有值,并且此函数仅给出日期。如果您尝试像

it gives error because mktime function require all values of numbers only and this function gives only date . if you try like

$h = 18;
$i = 11;
$s = 00;
$m = 07;
$d =23;
$y = 2009;
echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y));

然后它将工作。

so您的完整代码将是

so your complete code will be

date_default_timezone_set('UTC');

$d = str_replace('/', ',', '07/23/2009');
$t = str_replace(':', ',', '18:11');
$date = $t.',0,'.$d;
$fulldate = explode(',',$date);
echo '<br>';
$h = $fulldate[0];
$i = $fulldate[1];
$s = $fulldate[2];
$m = $fulldate[3];
$d =$fulldate[4];
$y = $fulldate[5];

echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y)) . "<br>";

//如果你想要时间戳
然后使用

//if you want timestamp then use

echo strtotime("07/23/2009 18:11");

谢谢

这篇关于如何将日期和时间转换为php中的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆