PDA

View Full Version : PHP MySql Date



KobusDJ
25-06-2009, 12:50 PM
I have a MySql table with a date field.

I can display the date: echo $row['TheDate'];

but need to know the year.


How do I get the Year value (e.g. 2009) from: $row['TheDate'];

Thanks.

dyanamo
25-06-2009, 01:49 PM
The quickest and simplist way I can think for you is to do :
year = substr($row['TheDate'],0,4);
I say this since mysql (if using the date type) store the date in the format 2009-10-10.

Kloon
25-06-2009, 01:55 PM
SELECT YEAR(datefield) AS theyear FROM table

$row['theyear']

KobusDJ
25-06-2009, 02:33 PM
Thanks dyanamo. The string manipulation works. This way I can get the month and day as well. Super!

Kloon, is there an equivalent for the MONTH and DAY? Your example works well to get the YEAR.

dyanamo
25-06-2009, 02:48 PM
Thanks dyanamo. The string manipulation works. This way I can get the month and day as well. Super!

Kloon, is there an equivalent for the MONTH and DAY? Your example works well to get the YEAR.

Just replace YEAR with MONTH or DAY :)

KobusDJ
25-06-2009, 03:05 PM
Ok, YEAR, MONTH and DAY works fine individually, but how do I get all three in one SELECT.

Something like this:
SELECT YEAR(TheDate) AS TheYear, MONTH(TheDate) AS TheMonth, DAY(TheDate) AS TheDay FROM MyTable


....doesn't work

dyanamo
25-06-2009, 03:13 PM
Rather than getting into mysql functions rather use the substr meathod especially if you want to retrieve multiple fields.

KobusDJ
25-06-2009, 03:20 PM
I have already implemented the substring method.

Just curious why the sql function doesn't work.

I found this as well (http://www.simplemachines.org/community/index.php?topic=22517.0)

dequadin
25-06-2009, 03:27 PM
I have already implemented the substring method.
Just curious why the sql function doesn't work.


The substring method is dangerous because it could give varying answers depending on the Regional/Language options that are set-up.

I speak purely from a Windows Development point of view. Rather get the SQL Build in functions to work, could save you some hassle later...

KobusDJ
25-06-2009, 03:55 PM
I do not see a problem with extracting the string.

The php is running on a Linux server and AFAIK (I may be wrong), the format in a Date field in MySQL is always YYYY-MM-DD.

Only time there will be problem, is when my host (LunarPages) starts to stuff around with the date format on their webservers.

dequadin
25-06-2009, 04:09 PM
I do not see a problem with extracting the string.

The php is running on a Linux server and AFAIK (I may be wrong), the format in a Date field in MySQL is always YYYY-MM-DD.

Only time there will be problem, is when my host (LunarPages) starts to stuff around with the date format on their webservers.

Looks like that is not the case (http://lists.mysql.com/win32/16795)!

Why are you so against doing things properly? Code like this (substring-ing a year out of a date) rubs me up the wrong way...

KobusDJ
25-06-2009, 04:17 PM
Do you know why.....

SELECT YEAR(TheDate) AS TheYear, MONTH(TheDate) AS TheMonth, DAY(TheDate) AS TheDay FROM MyTable

..... doesn't work.

I'm not changing the date format on the server..... the string will work for now.

EDIT: I see the link you posted is on the MySQL on Win32 section.

dequadin
25-06-2009, 04:23 PM
Do you know why.....

SELECT YEAR(TheDate) AS TheYear, MONTH(TheDate) AS TheMonth, DAY(TheDate) AS TheDay FROM MyTable

..... doesn't work.

I'm not changing the date format on the server..... the string will work for now.

I did read the thread before I posted so I know that it didn't work for you.

What TYPE is your TheDate column? Maybe you need to cast it first?

Something like
SELECT YEAR(str_to_date(TheDate )) AS theYear FROM MyTable

KobusDJ
25-06-2009, 04:35 PM
The TheDate field is of type 'Date' in the MySql table.

SELECT YEAR(TheDate) AS TheYear FROM MyTable ....works fine
SELECT MONTH(TheDate) AS TheMonth FROM MyTable .... works fine
SELECT DAY(TheDate) AS TheDay FROM MyTable ... works fine

The problem comes when I attempt to get all three values in on SELECT statement.

dequadin
25-06-2009, 04:56 PM
The TheDate field is of type 'Date' in the MySql table.

SELECT YEAR(TheDate) AS TheYear FROM MyTable ....works fine
SELECT MONTH(TheDate) AS TheMonth FROM MyTable .... works fine
SELECT DAY(TheDate) AS TheDay FROM MyTable ... works fine

The problem comes when I attempt to get all three values in on SELECT statement.

Had a quick look around on Google and it should be possible, so your problem is elsewhere.



SELECT YEAR(your_date) Y, MONTH(your_date) M, DAY(your_date) D FROM your_table;

Is a perfectly valid MySQL query according to this (http://dev.mysql.com/doc/refman/5.0/en/select.html), this (http://www.techotopia.com/index.php/Working_with_Dates_and_Times_in_MySQL), this (http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_extract) and this (http://innovationsopen.com/?p=48).

Something you could try is:


SELECT EXTRACT(YEAR FROM your_date) AS Y, EXTRACT(MONTH FROM your_date) AS M, EXTRACT(DAY FROM your_date) AS D FROM your_table;

KobusDJ
25-06-2009, 07:13 PM
Mmmmm.... I retyped the procedure from scratch at home tonight. Sql query works fine with all three in the SELECT.

Will have to drink more coffee at the office.

Thanks everyone for the help.... (and persistency to do it the PROPER way :D )

dequadin
25-06-2009, 08:16 PM
Mmmmm.... I retyped the procedure from scratch at home tonight. Sql query works fine with all three in the SELECT.

Will have to drink more coffee at the office.

Thanks everyone for the help.... (and persistency to do it the PROPER way :D )

Glad you got it right. It's always worth it to spend extra effort and to try and do things properly, will have you a lot of headache in the long run.

FarligOpptreden
25-06-2009, 09:33 PM
Why are you so against doing things properly? Code like this (substring-ing a year out of a date) rubs me up the wrong way...

OOH! I absolutely hate substringed dates. In SQL Server, datediff and dateadd are your best friends...

dequadin
26-06-2009, 08:39 AM
OOH! I absolutely hate substringed dates. In SQL Server, datediff and dateadd are your best friends...

Where were you yesterday when I was fighting the good fight? :D

FarligOpptreden
26-06-2009, 08:50 AM
Where were you yesterday when I was fighting the good fight? :D

Earning my salary... ;)

dequadin
26-06-2009, 08:53 AM
Earning my salary... ;)

Hahahaa good answer! :cool: