How do you extract the MONTH from a date variable?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you extract the MONTH from a date variable?

Post by simonmlewis »

Code: Select all

$month = date("F", $row->date);
If $row->date is 'February', I thought this code would set $month to February.

Code: Select all

	echo date("F", $row->date);
This works a treat, by showing February, but I want to put it into a variable.

Can someone tell me how, as I cannot trace this in a search....
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do you extract the MONTH from a date variable?

Post by John Cartwright »

Code: Select all

$month = date("F", $row->date);
?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract the MONTH from a date variable?

Post by simonmlewis »

Huh?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do you extract the MONTH from a date variable?

Post by John Cartwright »

Obviously I misunderstood what you are asking.

Please re-phrase your question.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract the MONTH from a date variable?

Post by simonmlewis »

Ok - sorry.
I can extract the date from the database and get the F (month) out of it and echo it direct to the page.
But how do I STORE IT IN A VARIABLE?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do you extract the MONTH from a date variable?

Post by John Cartwright »

Ok so I did understand you correctly. If you want to store it in a variable, instead of echo'ing it, then as shown before

Code: Select all

$month = date("F", $row->date);
Stores the month into the $month variable...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract the MONTH from a date variable?

Post by simonmlewis »

Mmmm
This is what I already have, but it just won't do it.
$row->date, or the "date" field, isn't a protected name is it??
When I echo $month, all I get is "January", and if I echo the same for $year, I get 1970.

Code: Select all

			$month = date("F", $row->date);
	$year = date("Y", $row->date);
ALSO... if I just echo $row->date, I get "2011-04-01".
So I know it's correct.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract the MONTH from a date variable?

Post by simonmlewis »

Code: Select all

	$nmonth = date("F", $row->date);
	$nyear = date("Y", $row->date);
	echo "$nmonth, $nyear<br/>$row->date";
[text]January, 1970
2011-04-01[/text]

This is just not cricket!!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do you extract the MONTH from a date variable?

Post by AbraCadaver »

Code: Select all

$month = date("F", strtotime($row->date));
Or you could return it as UNIX_TIMESTAMP in the query, then your original code would work.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract the MONTH from a date variable?

Post by simonmlewis »

Well that works perfectly - but how does that work, when the code that's erroring, doesn't?

I don't get it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do you extract the MONTH from a date variable?

Post by AbraCadaver »

date() requires a unix timestamp. 2011-04-01 is not a unix timestamp, 1301634000 is. So strtotime() gives you the unix timestamp from the date string.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract the MONTH from a date variable?

Post by simonmlewis »

but why does the straightward echo work (see first post of this thread), yet not allow you to put that echo straight into a variable?

Sorry if your last comment answers that, but I still don't get it. As by your statement, you'd have to convert it to unix timestamp to even just echo it ... wouldn't you?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do you extract the MONTH from a date variable?

Post by AbraCadaver »

Dunno, it shouldn't work:

Code: Select all

$month = date("F", '2011-04-01');
echo $month . "\n";
echo date("F", '2011-04-01') . "\n";
[text]Notice: A non well formed numeric value encountered in /home/shawn/www/test.php on line 1
December

Notice: A non well formed numeric value encountered in /home/shawn/www/test.php on line 3
December[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you extract the MONTH from a date variable?

Post by simonmlewis »

Actually now... .it doesn't work the other way. I swear it did. But something peculiar, and now just using:

echo date("F", strtotime($row->date));

It is fine. So we are good now. Thanks for the support peeps.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply