Page 1 of 1
How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 10:12 am
by simonmlewis
If $row->date is 'February', I thought this code would set $month to February.
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....
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 10:22 am
by John Cartwright
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 10:24 am
by simonmlewis
Huh?
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 10:25 am
by John Cartwright
Obviously I misunderstood what you are asking.
Please re-phrase your question.
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 10:27 am
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?
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 10:31 am
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
Stores the month into the $month variable...
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 10:34 am
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.
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 11:14 am
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!!!
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 11:23 am
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.
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 11:48 am
by simonmlewis
Well that works perfectly - but how does that work, when the code that's erroring, doesn't?
I don't get it.
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 11:53 am
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.
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 11:56 am
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?
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 12:04 pm
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]
Re: How do you extract the MONTH from a date variable?
Posted: Tue Mar 15, 2011 12:18 pm
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.