I currently have some code to show the latest update on my site.
I have two problems:
1) how do I get the date to Display "04-13-10". I know the php code is date("m-d-y") But how would I incorporate that for an updated item?

2) limit the number of updates to display to 3 or 5?

Current Code:

$select = ("SELECT * FROM photo_albums ORDER BY uptime ASC ");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
//I want the date to be displayed here un $row['uptime']
echo "<p align='center'>".$row['uptime']."</p>"
    ."<p align='left'><a href='viewAlbum.php?aid=".$row['aid']."'>".$row['description']."</a></p>"
	."<hr width='75%' color='#FFFFFF' />";
}

Not sure what your after with the first question, do you want to change the format of the date ie m/d/y or d/m/y ?

For question 2 you basically answered that yourself. put a limit in the sql query

$select = ("SELECT * FROM photo_albums ORDER BY uptime ASC LIMIT 0,5 ");

should do the trick...

Hope it helps....

#### EDIT #####

If i'm reading correctly you want to pull the newest entry first? maybe try the order by as DESC.

Hope it helps...

// 1) Im not sure im following you but im thinking this... 
$timestamp = $row['uptime'];
echo date("m-d-y h:i a",$timestamp);
// 2) 
$select = ("SELECT * FROM photo_albums ORDER BY uptime ASC LIMIT 3"); 
// or whatever amount

The reason I want it to show up like 04-13-10 is because Now people would see this "2009-10-21 17:58:19".

I tried this code and now I see this"12-31-69 07:00 pm" above one post but not any of the others.

$timestamp = $row['uptime'];
echo date("m-d-y h:i a",$timestamp);

This is what I have been working with and it still does not work..
any suggestions?

$timestamp = $row['uptime'];
echo date("m-d-y",$timestamp);
$select = ("SELECT * FROM photo_albums ORDER BY uptime DESC LIMIT 0,3 ");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<li>".$timestamp."</li>";
}
//Also this
echo "<li>".date("m-d-y",$timestamp)."</li>";

This is what I see both times:
12-31-69

let me try to help you on the date problem...

first of all using the date() function make sure your variable $timestamp is a unix timestamp and not just a date. a unix timestamp is the number of seconds since jan 1, 1970. use the date function like you're trying

echo date("m-d-y", 385538400); // 03-21-82
echo date("m-d-y", $timestamp); // 03-21-82
echo date("n-j-Y", $timestamp); // 3-21-1982

check out http://us2.php.net/manual/en/function.date.php for the full list of choices.

Secondly the most recently added:

first use DESC at the end so already the newest are first then limit them to 5 or so. ...DESC LIMIT 5;

SELECT * FROM photo_albums ORDER BY uptime DESC LIMIT 5

You can add in the 0 but it's going to default to the first 5 anyways.

Good luck

Well i am still still stuck. The date still displays at "12-31-69" and if I remember correctly it should say "10-21-09"
I have never used the date function before so I cannot tell you what my issue is here just show you my code and hope for the best.
I will put the date in bold:

$timestamp = $row['uptime'];
//echo date("m-d-y",$timestamp);
$select = ("SELECT * FROM photo_albums ORDER BY uptime DESC LIMIT 5 ");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<li>"
    ."<p align='left'><a href='viewAlbum.php?aid=".$row['aid']."'>"
	."<img src='".$row['filepath'].$row['filename']."' width='75px' align='left' border='1'/></a>"
	."<span class='lpBold'>".[B]date("m-d-y",$timestamp)[/B]."</span><br/>"
	."<a href='viewAlbum.php?aid=".$row['aid']."'>".$row['description']."</a></p>"
	."</li><hr width='75%' color='#FFFFFF' />";
}
$select = ("SELECT DATE_FORMAT(%c-%d-%y) as theDate, * FROM photo_albums ORDER BY uptime DESC LIMIT 5 ");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<li>"
    ."<p align='left'><a href='viewAlbum.php?aid=".$row['aid']."'>"
	."<img src='".$row['filepath'].$row['filename']."' width='75px' align='left' border='1'/></a>"
	."<span class='lpBold'>". $row['theDate'] ."</span><br/>"
	."<a href='viewAlbum.php?aid=".$row['aid']."'>".$row['description']."</a></p>"
	."</li><hr width='75%' color='#FFFFFF' />";
}
$select = ("SELECT DATE_FORMAT(%c-%d-%y) as theDate, * FROM photo_albums ORDER BY uptime DESC LIMIT 5 ");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<li>"
    ."<p align='left'><a href='viewAlbum.php?aid=".$row['aid']."'>"
	."<img src='".$row['filepath'].$row['filename']."' width='75px' align='left' border='1'/></a>"
	."<span class='lpBold'>". $row['theDate'] ."</span><br/>"
	."<a href='viewAlbum.php?aid=".$row['aid']."'>".$row['description']."</a></p>"
	."</li><hr width='75%' color='#FFFFFF' />";
}

:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%c-%d-%y) as theDate, * FROM photo_albums ORDER BY uptime DESC LIMIT 5' at line 1

are you using a UNIX TIME STAMP in your database for the date added???

try to print out the date without formatting it with the date() function... and what do you get?

$sql = "SELECT * FROM photo_albums ORDER BY uptime DESC LIMIT 5";
$query = mysqli_query($sql);
hile ($row = mysqli_fetch_array($query)){
      
     //just a test
    printf($row['aid'] . "<br>");
    printf($row['filepath'] . "<br>");
    printf($row['filename'] . "<br>");
    printf($row['uptime'] . "<br>"); //test the format of the date (should be unix timestamp ie. 1271221200 for today)
    printf(date("m-d-y",$row['uptime']) . "<br>");
    printf($row['id'] . "<br>");
      
}

When I had it at "unix timestamp" it would set the date to all "Zeros" so I changed it to "NOW" and it sets the current date. Just so you know I am adding my galleries through phpmyadmin and adding my photos through CSV excel file.

Is there a way to get unix timestamp to work if I am importing directly through phpmyadmin? Or does it take some time for it to display the proper date?

ok I stand incorrect I just double checked my table structure and I do have uptime set to TIMESTAMP and when I upload something new I set it to now for the current time.

Member Avatar for soldierflup

If the date in the database is set as date (in mysql this will show 2010-04-14), then you have to use the function strtotime to convert the date.

echo date('Y m d', strtotime($row['datefield-in-db']));

Hope this helps to.

Finally a break though! thanks soldierflup.

Member Avatar for soldierflup

You're welcome :)

:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%c-%d-%y) as theDate, * FROM photo_albums ORDER BY uptime DESC LIMIT 5' at line 1

Sorry, line 1 should have been:

$select = ("SELECT DATE_FORMAT('%c-%d-%y') as theDate, * FROM photo_albums ORDER BY uptime DESC LIMIT 5 ");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.