开发者

working with dates

I was wondering if anyone would help with the following:

I have a date called into a function in php. I need to estrapolate the month form this date in order to make some calculation base on the month only.

I really have no idea how do go about it. I have tried few things but none works.

It there anyone who ca开发者_开发问答n give a clue?

Appreciated any little help. Francesco


You need a unix timestamp to extract that information. Either you already have such a value, or you can get it using strtotime() on a string. The function you need to extract the month is date():

date('n', $timestamp);


Simple

$month= date('n',strtotime($input));


You need to do two steps: firstly convert the your input string into a datetime object PHP can work with, and secondly, extract the month from that dateime.

$timestamp = strtotime($inputDate);
$month = strftime("%m", $timestamp);

This would give you the month as a two digit number. There are more options in the full documentation for strftime

strtotime() should cope with the vast majority of date formats automatically. If it can't cope with your input then you need to use strptime() and pass in your exact format

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜