Reputation: 9
When a image is uploaded to the site I want to display the month which it was uploaded under the picture. I have a time-stamp field in the database.
Can I get the month from the time stamp if it is possible how can I do that?
I tried to do it using this code
<?php $query="SELECT * FROM magimgdes ORDER BY id DESC, time DESC " ;
$result=mysql_query($query) OR DIE(mysql_error());
while($row=mysql_fetch_array($result)){
$value1=$row['image'];
$value3=$row['time'];
?>
<img src="admin/upload/hompageimage/<?php echo $value1; ?>" width="180" height="195" alt="magimage" />
<?php echo $value3;} ?></div>
The "time" field is the timestamp field in database.
I just want to echo only the month.
Upvotes: 0
Views: 90
Reputation: 11026
First you will need to convert the MySql datetime format to UNIX timestamp, to do that you can use strtotime
function. Then you can use many functions to deal in the extraction of the month, for example getdate
function.
$timestamp = strtotime($row['time']); // Convert to unix time.
$info = getdate($timestamp); // Get the fields of a date (in an array).
$month = $info['mon']; // This is the element that contains the month.
echo $month; // This will print a number from 1 to 12.
Upvotes: 0
Reputation: 1825
You can do that with the date function.
http://php.net/manual/en/function.date.php
Upvotes: 0
Reputation: 57322
Yes , you can get by date()
$weekday = date('N', $timestamp);
$month = date('m', $timestamp);
$day = date('d', $timestamp);
Upvotes: 1