简体   繁体   中英

php - Convert month number to month short name while I am calling it from Mysqli

I am calling the data from Mysqli and in my table it appear like this 2012-10-05 I need to call it near my new to be display like this <span class="date"><strong>28</strong><span>may</span></span> no I have two values one is the day and the other is month and also I'd like it to appear as number for day and short name for month eg.Feb May

this is my PHP code

<?php
    $getNews="SELECT * FROM news";
    $QgetNews=$db->query($getNews)or die($db->error);
?>

You can use date() and strtotime() for that.

<?php
// $QgetNews->date = 10-05-2012
echo date('d M, Y', strtotime($QgetNews->date)); // 10 May, 2012

CodePad

Im not 100% sure how your data looks but if it's 2012-10-05 like you say then check out Converting date to timestamp in PHP

Then take that timestamp and feed into date() or a shorter version: $month = date("M", strtotime($date));

which will return the shortname of the month. Look at PHP Manual Date() for more of the parameters that can be used to get the day, year etc... You can call each one individually like in my example, or you could comma separate and explode the variable into an array if you really wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM