Jump to content

Day of the month conversion


Drummin

Recommended Posts

Hello,

Doing a little calendar where days are incremented as variable $i formatted like 'j' or single digit numbers 1,2,3 etc.  (The calendar works fine)

However for querying DB tables I need the day formatted as 'd' or 01,02,03 etc. so on any day I need to make this conversion.

 

I can't find an easy solution though there must be one.  Thanks in advance.

<?php
//test day
$i=7;
$day=date('d', mktime($i));
echo "$day";
//Should be 07
?>

Link to comment
Share on other sites

You could do it in the query string rather simply also with MySQL's STR_TO_DATE() function. This will accept the days/months with with or without the leading zeros.

 

$date = '2010-2-9'; // OR $date = '2010-02-09';
$query = "SELECT field FROM table WHERE date_field = STR_TO_DATE('$date', '%Y-%c-%e')";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.