如何仅在 PHP 中显示日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10226627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:42:03  来源:igfitidea点击:

How to display Date only in PHP?

phpdatetime

提问by srahifah

How to display date only(without time) with format (Y:m:d) for table Return Date? When I choose the date in sql, it'll view as picture1(eg: 2012-04-10), after I save and display back it'll view as picture2(eg: Apr 10 2012 12:00:00). I would like to how to display as(eg: 2012-04-10) not display as (eg: Apr 10 2012 12:00:00) in table Return Date. Thanks

如何为表返回日期仅显示日期(无时间)和格式(Y:m:d)?当我在sql中选择日期时,它会显示为picture1(例如:2012-04-10),保存并显示回来后它会显示为picture2(例如:Apr 10 2012 12:00:00)。我想如何在表返回日期中显示为(例如:2012-04-10)而不是显示为(例如:Apr 10 2012 12:00:00)。谢谢

Picture 1

图片1

enter image description here

在此处输入图片说明

Picture 2

图2

enter image description here

在此处输入图片说明

under Result.php

在 Result.php 下

$query = "SELECT ....BE.[batch_id],BED.[ReturnDate] 
FROM .... as BE 
INNER JOIN .... as BED
ON BE.[batch_exception_id] = BED.[batch_exception_id]
WHERE BE.[process_date_time] between '$date1' and '$date2'"; 

while ($row = mssql_fetch_assoc($result)) {   
$rDate = $row['ReturnDate'];
$beID = $row['batch_exception_id'];                         
$proc_dt = $row['process_date_time'];
$batchid = $row['batch_id'];
echo "<tr>";
echo "<td>" . $beID . "<input type='hidden' name='beID[$i]' value='$beID'/></td>";
echo "<td>" . $batchid . "<input type='hidden' name='batchid[$i]' value='$batchid'/></td>";
echo "<td>" . $proc_dt . "<input type='hidden' name='procDT[$i]' value='$proc_dt'/></td>";

echo "<td>";
$date="date[".$i."]"; 
echo "<input type='text' name='date[$i]' id='$date' value='$rDate' onclick=\"fPopCalendar('".$date."')\">";
$i++;
echo "</td>";
echo "</tr>";   
}

Under Save.php

在 Save.php 下

//Select database
$selected = mssql_select_db($myDB, $link)or die("Couldn't open database $myDB");

for ($i=0; $i<$tot_rec;$i++) {
$ret_date=trim($arrretDate[$i]);
ECHO "$ret_date<br>";
if (strlen($ret_date)>0) {
$query = "UPDATE .....
SET [ReturnDate] = '$ret_date'
WHERE [batch_exception_id]= '$arrbeID[$i]'";

$result = mssql_query($query);}
}
//execute the SQL query                     
if ($result){   
header("Location:....");}
else{   
echo "Error Save";   
}

回答by gmaliar

You can try this date('Y:m:d', strtotime($date));

你可以试试这个 date('Y:m:d', strtotime($date));

回答by Mohamed Hassan

You can use: strftime("%Y-%m-%d",$date);. Also check thislink.

您可以使用:strftime("%Y-%m-%d",$date);。另请检查链接。

Content from documentation:

文档中的内容:

strftime— Format a local time/date according to locale settings

Description:string strftime ( string $format [, int $timestamp = time() ] )

strftime— 根据区域设置格式化本地时间/日期

描述:string strftime ( string $format [, int $timestamp = time() ] )

%Y  Four digit representation for the year  Example: 2038
%m  Two digit representation of the month   01 (for January) through 12 (for December)
%d  Two-digit day of the month (with leading zeros) 01 to 31

回答by Vaishu

Try this..

尝试这个..

SELECT DATE( ReturnDate ) as dat FROM Batch

Return Date : sep 12 2012 12:24:12

返回日期:2012 年 9 月 12 日 12:24:12

dat : sep 12 2012

日期:2012 年 9 月 12 日

回答by Tuhin Subhra Dey

You can use the date()function to display the date in any format.

您可以使用date()函数以任何格式显示日期。