Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm querying a MySQL database and displaying the info in order of timestamp (DESC). Essentially, I'm grabbing the last record in the database, displaying it's date and year as the header of it's section and then going backwards from there. I've put together these if and else if statements to display the appropriate month name according to the timestamp. It works fine but I know I could probably do this with an array or some sort of other loop. I'm still learning this type of coding so I was wondering if someone could help me find a way to make this code more efficient. Thanks!

$sql = "SELECT * FROM user ORDER BY timestamp DESC LIMIT 1";
$result = $mysqli->query($sql);
$row_lastone = mysqli_fetch_array($result);
$timestamp_lastone = date_parse($row_lastone['timestamp']);
$month_lastone = $timestamp_lastone['month'];
$year_lastone = $timestamp_lastone['year'];
if ($month_lastone==1){
$lastone_month = "January";
}
else if ($month_lastone==2){
$lastone_month = "February";
}
else if ($month_lastone==3){
$lastone_month = "March";
}
else if ($month_lastone==4){
$lastone_month = "April";
}
else if ($month_lastone==5){
$lastone_month = "May";
}
else if ($month_lastone==6){
$lastone_month = "June";
}
else if ($month_lastone==7){
$lastone_month = "July";
}
else if ($month_lastone==8){
$lastone_month = "August";
}
else if ($month_lastone==9){
$lastone_month = "September";
}
else if ($month_lastone==10){
$lastone_month = "October";
}
else if ($month_lastone==11){
$lastone_month = "November";
}
else if ($month_lastone==12){
$lastone_month = "December";
}
echo "<h1>".$lastone_month." ".$year_lastone."</h1>";

Thanks for the help!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Try something like this:

$sql = "SELECT * FROM user ORDER BY timestamp DESC LIMIT 1";
$result = $mysqli->query($sql);
$row_lastone = mysqli_fetch_array($result);

$date = date("M Y", strtotime($row_lastone['timestamp']));

echo "<h1>".$date."</h1>";
share|improve this answer

One possible solution would be to let your DB do the work.

SELECT DATE_FORMAT(´timestamp´, '%M') FROM user ORDER BY timestamp DESC LIMIT 1 

see http://dev.mysql.com/doc/refman/5.1/de/date-and-time-functions.html

Also i would suggest to not name your col 'timestamp', it may not be a reservered word but it could lead to confusion or forcing you to escape your col in your queries.

EDIT

if you want to stay in PHP or your DB does not support my suggested MySQL solution you could use DateTime to format your timestamp

$date = new DateTime();
$date->setTimestamp($timestamp_lastone);
echo $date->format('F');

see http://www.php.net/manual/de/class.datetime.php

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.