YYY / MM / DD到DD / MM / YY PHP

时间:2018-10-10 07:34:08

标签: php

<div class="table-responsive">  
<table id="Well_CAT" class="table table-striped table-bordered">                              
<thead> <th>Client_Contract_Number</th>
<th>Currently_Using</th>
<th>MBPS_EAM_Number_RIGT</th>
<th>Model_and_Type</th>
<th>LFour_Yearly</th>
<th>Six_Monthly</th>
<th>One_Yearly</th>
<th>One_and_Half_Yearly</th>
<th>Two_Yearly</th>
<th>Two_and_Half_Yearly</th>
<th>Three_Yearly</th>
<th>Three_and_Half_Yearly</th>
<th>Four_Yearly</th>
<th>Remarks</th>
</thead>  
<?php
while($rows=mysql_fetch_array($result)){
?><tr>  
 <td class="exdate"><? echo $rows['Client_Contract_Number']; ?></td>
 <td class="exdate"><? echo $rows['Currently_Using']; ?></td>
 <td><? echo $rows['MBPS_EAM_Number_RIGT']; ?></td>
 <td><? echo $rows['Model_and_Type']; ?></td>
 <td><? echo $rows['LFour_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Six_Monthly']; ?></td>
 <td class="exdate"><? echo $rows['One_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['One_and_Half_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Two_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Two_and_Half_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Three_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Three_and_Half_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Four_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Remarks']; ?></td>
  </tr>  
    <?php
     }
     ?>
  </table>

我的表格日期为YYY / MM / DD格式,我想将其更改为DD / MMM / YY格式,我设法对每一列进行了设置。一次全部摆好桌子。 (我要处理的表超过15张)请使用同一示例帮助我..以便我可以对所有其他示例进行处理。

4 个答案:

答案 0 :(得分:2)

<?php 

// function to convert string and print 
function convertString ($date) 
{ 
    // convert date and time
    $sec = strtotime($date); 

    // convert seconds into a specific format 
    $date = date("d-m-Y", $sec); 

    // append seconds to the date
    $date = $date; 

    // print final date
    echo $date; 
} 

 // Driver code 
 $date = "2014/12/06"; 
 convertString($date); 
?> 

您可以将变量传递给此函数,该函数将转换您的日期格式

答案 1 :(得分:1)

您可以做的是,可以使用PHP中的 DateTime 对象并将其转换为所需的格式。

步骤:

1)声明您当前的日期格式(以您的情况为Y-m-d)。

2)使用日期格式变量和数据库中的日期创建一个 DateTime 对象。

3)将 DateTime 对象转换为所需的格式(d / m / Y)。

代码示例:

<?php
//Variable simulating your current Y-m-d format
$dateFromDatabase=date('Y-m-d');

//Example of date formating/conversion:

//The current format
$format = "Y-m-d";

//Creates DateTime object for conversion
$timedate = DateTime::createFromFormat($format, $dateFromDatabase);

//Perform the format of the DateTime object
$dateFromDatabase = $timedate->format('d/m/Y');

//Result
echo $dateFromDatabase;
?>

构造函数以实现相同功能的示例:

<?php
function convertMyDate($dateVar) {
    $format = "Y-m-d";
    $timedate = DateTime::createFromFormat($format, $dateVar);
    $dateVar = $timedate->format('d/m/Y');

    return $dateVar;
}
?>

该功能的使用示例:

<?php
//Variable simulating your current Y-m-d format
$dateFromDatabase=date('Y-m-d');

echo convertMyDate($dateFromDatabase);
?>

两个示例here的实时示例。

答案 2 :(得分:0)

尝试一下:

<?php
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
?>

答案 3 :(得分:0)

只需更改一行代码就可以完成。

// Ensure the variable date here on the right is
your unformatted date. i.e. Make sure it's the date value you've got from your row.

echo date("d-m-Y", strtotime($date));

或者只是为其分配一个变量,然后回显以应用更改。

// Ensure the variable date here on the right is
your unformatted date. i.e. Make sure it's the date value you've got from your row.

$date = ("d-m-Y", strtotime($date)); 
echo $date;
相关问题