954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

selecting the day from the DATE field so that it includes a zero...

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

Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

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.

binoj_daniel
Practically a Master Poster
645 posts since Dec 2006
Reputation Points: 25
Solved Threads: 18
 

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.

trudge
Junior Poster
178 posts since Sep 2007
Reputation Points: 18
Solved Threads: 20
 

Thanks for the help ;)

Much appreciated

Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

Thanks for the help ;)

Much appreciated

Did it work for you?

binoj_daniel
Practically a Master Poster
645 posts since Dec 2006
Reputation Points: 25
Solved Threads: 18
 
Did it work for you?


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

Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

What php code did you try?

binoj_daniel
Practically a Master Poster
645 posts since Dec 2006
Reputation Points: 25
Solved Threads: 18
 

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'];
}
Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You