francesco
francesco

Reputation: 13

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 can give a clue?

Appreciated any little help. Francesco

Upvotes: 1

Views: 160

Answers (3)

mth
mth

Reputation: 490

Simple

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

Upvotes: 1

soulmerge
soulmerge

Reputation: 75704

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);

Upvotes: 2

Ian
Ian

Reputation: 4258

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

Upvotes: 0

Related Questions