0

Is it possible to select day from the DATE field in a db so that it includes a zero in front of it if the day is less than 10?

If so could you post the code please.

Struggling and stressed...with qwerty imprinted on my forehead

3
Contributors
7
Replies
8
Views
10 Years
Discussion Span
Last Post by Venom Rush
0

You need to first convert the date to a php format. So we use the strtotime() function.

<?php
$date = "2007-11-13"; (or from DB)
$date = strtotime($date);
$aday = date("l",$date);

//Then we will pad zeros.

$paddedDays = str_pad($aday, 2, "0",STR_PAD_LEFT);
echo $paddedDays ;
?>

Let me know how it goes.

0

Or, to use the MySQL functions, you could do ...

mysql> select date_format("2007-1-1","%Y-%m-%d");
+------------------------------------+
| date_format("2007-1-1","%Y-%m-%d") |
+------------------------------------+
| 2007-01-01                         |
+------------------------------------+
1 row in set (0.25 sec)

Then the solution is more general, and could be used if you were using Perl or PHP or whatever.

0

Did it work for you?

Didn't try it out, managed to sort my problem out with some php code.

0

Used this for putting the date back into the db so I didn't actually need to pull it out with the zero.

if ($_GET['startDay'] = $row[0]) {
$day = $_GET['startDay'];
  if ($day < 10) {
    $startDay = "0".$day;
  } else {
  $startDay = $_GET['startDay'];
  }
} else {
$startDay = $_POST['startDay'];
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.