Retrieve Date in Specified Format

One of the MySQL functions which can be used is DATE_FORMAT. Below you will find an example. Note that the DATE_FORMAT appears in the array index in addition to the query string.

$query = "SELECT vin, title, cost, year, make, model, DATE_FORMAT(dateOfPurchase, '%m/%d/%Y'),
bodyNumber, transmissionSN, engineSN, knownAs, DATE_FORMAT(dateOfRemoval, '%m/%d/%Y'), isInFleet
            FROM vehicles
            WHERE tag = '$tag' AND state = '$state'";
$result = mysql_query($query);
if (!$result || mysql_num_rows($result) < 1) {
  die('Error 56801. -Programmer.');
} else {
  $user_array = mysql_fetch_array($result);
  $vin = $user_array['vin'];
  $title = $user_array['title'];
  $cost = $user_array['cost'];
  $year = $user_array['year'];
  $make = $user_array['make'];
  $model = $user_array['model'];
  $dateOfPurchase = $user_array['DATE_FORMAT(dateOfPurchase, \'%m/%d/%Y\')'];
  $bodyNumber = $user_array['bodyNumber'];
  $transmissionSN = $user_array['transmissionSN'];
  $engineSN = $user_array['engineSN'];
  $knownAs = $user_array['knownAs'];
  $dateOfRemoval = $user_array['DATE_FORMAT(dateOfRemoval, \'%m/%d/%Y\')'];
  $isInFleet = $user_array['isInFleet'];

About samehramzylabib

See About on
This entry was posted in Coding, Database and tagged , , , . Bookmark the permalink.