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

Recommended Answers

All 7 Replies

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.

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.

Thanks for the help ;)

Much appreciated

Thanks for the help ;)

Much appreciated

Did it work for you?

Did it work for you?

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

What php code did you try?

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'];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.