将8位整数转换成dd / mm / yyyy [英] Converting an 8-digit integer into dd/mm/yyyy
问题描述
所以我在PHP脚本中有这个功能,它应该把日期作为一个8位数的整数,如 01042012
并将其转换为 01/04/2012
显示。
So i have this function in my PHP script that is supposed to take the date as an 8-digit integer such as 01042012
and convert it to 01/04/2012
for displaying.
目前我正在尝试使用php的 date()
函数如下:
Currently i'm trying to use php's date()
function as follows:
$int = 01042012;
$date = date("d/m/Y", $int);
而不是 $ date
code> 01/04/2012 它显示为 13/01/1970
。
Instead of $date
having the value 01/04/2012
it shows as 13/01/1970
.
这是因为 date()
希望我传递给它的日期是在unix时间?如果是这样,可能还有一种更直接的方式来分割字符2和4之后的int,然后插入斜杠,然后重新组合?就像 explode()
一样,但是我没有字符可以分割。
Is this because date()
wants the date i pass to it to be in unix time? If so, would there perhaps be a more direct way to split the int after characters 2 and 4 and just insert the slashes, then recombine it? Like what explode()
does, but i have no character to split with.
推荐答案
一个简单的方法;
$int = 01042012;
$day = substr($int,0,2);
$month = substr($int,2,4);
$year = substr($int,4);
$date = $day.'/'.$month.'/'.$year;
这篇关于将8位整数转换成dd / mm / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!