-5
<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>

My table dates are in YYY/MM/DD format, i would like to change it it DD/MMM/YY format,I managed to do it for each column.. it would be better if i can do it for entire table all at once. (I have more than 15 tables to process) Please help me with this same example.. so that i can do it for all others.

Siji
  • 33
  • 6
  • are you try to convert formate date YYY/MM/DD to DD/MM/YY in PHP? – Ascaliko Oct 10 '18 at 07:37
  • Here: https://stackoverflow.com/questions/2487921/convert-date-format-yyyy-mm-dd-dd-mm-yyyy – Metatron5 Oct 10 '18 at 07:40
  • Yes.. but i need to convert the date format of the entire table at once. (Eg: 10-Oct-2018) – Siji Oct 10 '18 at 07:41
  • In the table, which one is date field? – Tamilvanan Oct 10 '18 at 07:45
  • @TamilvananN these are the date field : echo $rows['LFour_Yearly']; ?> echo $rows['Six_Monthly']; ?> echo $rows['One_Yearly']; ?> echo $rows['One_and_Half_Yearly']; ?> echo $rows['Two_Yearly']; ?> echo $rows['Two_and_Half_Yearly']; ?> echo $rows['Three_Yearly']; ?> echo $rows['Three_and_Half_Yearly']; ?> echo $rows['Four_Yearly']; ?> – Siji Oct 10 '18 at 09:45
  • You can change the date_format while fetching from MySQL. Ex: `select date_format('2018/10/10','%d/%b/%Y');` – Tamilvanan Oct 10 '18 at 09:55

4 Answers4

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); 
?> 

you can pass your variable to this function which will convert your date format

sayalok
  • 882
  • 3
  • 15
  • 30
1

What you could do, is that you could make use of the DateTime object in PHP and convert it into your desired format.

Steps:

1) Declare your current date format (Y-m-d in your case).

2) Create a DateTime object, using your date format variable and the date from the database.

3) Convert the DateTime object to your desired format (d/m/Y).

Code example:

<?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;
?>

Example constructing a function to achieve the same functionality:

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

    return $dateVar;
}
?>

Example of use of the function:

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

echo convertMyDate($dateFromDatabase);
?>

Live example of both examples here.

Martin
  • 2,326
  • 1
  • 12
  • 22
0

try this:

<?php
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
?>
Martin
  • 2,326
  • 1
  • 12
  • 22
0

This can be done in one line of code with a simple change.

// 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));

Alternatively just assigned it a variable and echo that to apply the change.

// 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;
cmprogram
  • 1,854
  • 2
  • 13
  • 25