limitphp Posted January 5, 2009 Share Posted January 5, 2009 I have a datetime field in mysql. On a certain page, I just want to display the year only. How do i do that? Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/ Share on other sites More sharing options...
rhodesa Posted January 5, 2009 Share Posted January 5, 2009 <?php $date = '2009-01-05 12:34:35'; print date('Y',strtotime($date)); //or list($year) = explode('-',$date); print $year; ?> Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730031 Share on other sites More sharing options...
flyhoney Posted January 5, 2009 Share Posted January 5, 2009 echo date('Y', strtotime($mysql_datetime)); Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730032 Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format SELECT date_format(`datefield`, '%Y') FROM table_name Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730034 Share on other sites More sharing options...
limitphp Posted January 5, 2009 Author Share Posted January 5, 2009 thanks! Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730038 Share on other sites More sharing options...
sniperscope Posted January 5, 2009 Share Posted January 5, 2009 You can use substr() also. $current_date = $row['date_time_field']; $year = substr($current_date, 0, 10); //we assume your time stamp is 0000/00/00 00:00:00 echo $year; // so we have 0000/00/00 for output. Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730039 Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 Just a side note for you limit, pulling it out of MySQL in the correct format is the preferred way as it is more efficient and produces much less code. If you want to add more code, I would go with flyhoney's way. Just an fyi =) (Why write code to process it when you can pull it out exactly how you need it?) Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730042 Share on other sites More sharing options...
limitphp Posted January 5, 2009 Author Share Posted January 5, 2009 Just a side note for you limit, pulling it out of MySQL in the correct format is the preferred way as it is more efficient and produces much less code. If you want to add more code, I would go with flyhoney's way. Just an fyi =) (Why write code to process it when you can pull it out exactly how you need it?) How would I pull just the year out of the table? Yes, its in 2008-11-02 08:10:25 format. Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730047 Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format SELECT date_format(`datefield`, '%Y') FROM table_name ummm....see above? Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730051 Share on other sites More sharing options...
rhodesa Posted January 5, 2009 Share Posted January 5, 2009 SELECT date_format(`datefield`, '%Y') FROM table_name agreed Quote Link to comment https://forums.phpfreaks.com/topic/139558-solved-how-do-i-display-just-the-year-with-a-datetime-field/#findComment-730055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.