0

How do i formatt the dates do the come out as DD/MM/YYYY please

<?php

  $convar = mysqli_connect("localhost","root","","ffp_calendar");

  if(mysqli_connect_errno())
  {
    echo "Error occured while connecting with the database ".mysqli_connect_errno();
  }

  $sqlvar = "SELECT * FROM `calendar` WHERE `CurrentDate` = curdate()";

  $resultvar = mysqli_query($convar, $sqlvar) or die("Bad Query: $sql");

  while($rowvar = mysqli_fetch_assoc($resultvar)) {

    $todaysdate = ($rowvar['CurrentDate']); 
    $weekdate = ($rowvar['WeekStartDate']); 
    $perioddate = ($rowvar['PeriodStartDate']); 

  echo "<strong>Today's Date</strong>{$todaysdate}</br>";

  echo "<strong>This Week's Start Date</strong>{$weekdate}</br>";

  echo "<strong>This Week's Start Date</strong>{$perioddate}</br>";

}

?>
4
Contributors
3
Replies
29
Views
4 Weeks
Discussion Span
Last Post by benanamen
1

The example at Rproffitt's link is on the right track. You'll want to reference PHP's Date/Time Supported Date & Time Formats list to ensure MySQL's CURDATE() function returns a date formatted in a manner that works.

Thankfully CURDATE()'s standard output is YYYY-MM-DD is an OK format to work with.

$format = 'd/m/y';

$todaysdate = new DateTime($rowvar['CurrentDate']);
$weekdate = new DateTime($rowvar['WeekStartDate']); 
$perioddate = new DateTime($rowvar['PeriodStartDate']); 

echo $todaysdate->format($format);
echo $weekdate->format($format);
echo perioddate->format($format);

Now obviously I pulled the format out and stored it in a variable, which you may or may not choose to do. If you're running different formats for the various dates, you wouldn't want to. But this way, if you are keeping them the same format, your code follows the DRY principle a bit more.

0

What you need to do is stop using SELECT * and specify the column names explicitly. Then just ask the database to give you the date the way you want it. There is no need for code gymnastics.

SELECT
    DATE_FORMAT(CurrentDate, '%d/%m/%Y' ) CurrentDate 
FROM
    calendar 
WHERE  
   CurrentDate = CURDATE( )
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.