开发者

PHP..How to print month only from a date variable

I have a date variable like 10-25-1998 and I only want to echo 10开发者_运维问答. Can anyone help me about it? Thanks...

$dateString=date("m-d-Y", mktime(0,0,0,10,25,1998));
echo date('m', $dateString); // not working here....will print out 01


strtotime will not recognize "m-d-Y" format, so the only way to get the month out of that string is to just parse it yourself, knowing that it's the first value in your string:

$dateString=date("m-d-Y", mktime(0,0,0,10,25,1998));
$t = explode('-',$dateString);
echo $t[0]; //month
echo $t[1]; //day
echo $t[2]; //year


$tomorrow = mktime(0,0,0,01,25,1998);
echo "Month is ".date("m", $tomorrow);

For more info look here: PHP Date() Function


$first2chars = substr($tomorrow, 0, 2);

picks the first 2 characters of the string $tomorrow

take care


You may want the getdate function:

$today = getdate();
echo $today["mon"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜