如何将日期(“j/n/y”)转换为epoch ?

时间:2022-06-05 16:04:20

I have some data that makes use of date("j/n/y") format i.e date for today is 23/1/15

我有一些数据使用日期("j/n/y")格式。今天的日期是23/1/15。

I have tried

我有试过

echo strtotime($today);

but this does not give me the timestamp i want.How would i convert a date in date("j/n/y") format to epoch?.

但这并没有给出我想要的时间戳。我如何将日期(“j/n/y”)格式转换为新纪元?

3 个解决方案

#1


5  

Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.

使用DateTime::createFromFormat()读取日期格式,然后使用DateTime::getTimestamp()将其格式化为时间戳。

$date  = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();

#2


1  

I think you're looking for the mktime function in PHP. It goes a little like this:

我认为你在寻找PHP中的mktime函数。它有点像这样:

$timestamp = mktime(0,0,0,0,0,0);

Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:

在哪里,按照顺序,论点是:小时、分、秒、月、日、年。所以,在你的情况:

$today = mktime(0, 0, 0, 1, 23, 2015); 
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)

If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:

如果您正在寻找一个动态的当前时间戳,您可以在每个mktime的参数中使用date()。例如:

$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.

But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.

但是,正如约翰·康德所说,它要求你在使用日期之前把日期分开,所以它可能不那么有效。

Hope that helps!

希望会有帮助!

#3


0  

Just to have another approach this one would be good for 85 more years.

再来一种方法,这种方法在85年的时间里都很好。

$date = date('j/n/y', time());

list($day, $month, $year) = explode("/", $date);

$date = "20" . $year . "-" . $month . "-" . $day;

echo date('m/d/Y', strtotime($date));

#1


5  

Use DateTime::createFromFormat() to read the date format and then use DateTime::getTimestamp() to format it as a timestamp.

使用DateTime::createFromFormat()读取日期格式,然后使用DateTime::getTimestamp()将其格式化为时间戳。

$date  = DateTime::createFromFormat('j/n/y', '23/1/15');
$epoch = $date->getTimestamp();

#2


1  

I think you're looking for the mktime function in PHP. It goes a little like this:

我认为你在寻找PHP中的mktime函数。它有点像这样:

$timestamp = mktime(0,0,0,0,0,0);

Where, in order, the arguments are: hour, minute, second, month, day, year. So, in your case:

在哪里,按照顺序,论点是:小时、分、秒、月、日、年。所以,在你的情况:

$today = mktime(0, 0, 0, 1, 23, 2015); 
// Would return the timestamp for Jan. 23rd, 2015 at 12:00:00 am (I think)

If you're looking for a dynamic right now timestamp, you may use date() in each of the arguments of mktime. For example:

如果您正在寻找一个动态的当前时间戳,您可以在每个mktime的参数中使用date()。例如:

$rightnow = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
// Would return the timestamp for Jan. 23rd, 2015 at 10:57:25 am.

But, as John Conde says, it requires you break apart the date before you can use it, so it may not be as efficient.

但是,正如约翰·康德所说,它要求你在使用日期之前把日期分开,所以它可能不那么有效。

Hope that helps!

希望会有帮助!

#3


0  

Just to have another approach this one would be good for 85 more years.

再来一种方法,这种方法在85年的时间里都很好。

$date = date('j/n/y', time());

list($day, $month, $year) = explode("/", $date);

$date = "20" . $year . "-" . $month . "-" . $day;

echo date('m/d/Y', strtotime($date));