简体   繁体   English

我从Mysqli调用时将月份号转换为月份简称

[英]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 我正在从Mysqli调用数据,并且在我的表中看起来像这样2012-10-05我需要在我的新数据附近调用它,以便像这样显示<span class="date"><strong>28</strong><span>may</span></span>不,我有两个值,一个是日,另一个是月,我也希望它以日的数字和月的短名称显示。例如,5月

this is my PHP code 这是我的PHP代码

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

You can use date() and strtotime() for that. 您可以为此使用date()strtotime()

<?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 我不是100%知道数据的外观,但是如果是2012-10-05,如您所说,请查看在PHP中将日期转换为时间戳

Then take that timestamp and feed into date() or a shorter version: $month = date("M", strtotime($date)); 然后使用该时间戳记并输入date()或更短的版本:$ 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. 查看PHP Manual Date()以获得更多可用于获取日,年等的参数。您可以像在我的示例中那样单独调用每个参数,也可以逗号分隔并将变量展开为数组,如果你真的想要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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