Jump to content

Recommended Posts

Ok I am trying to figure out the best way to format dates and times pulled from a db.
Basically what I am doing is making a form to edit an event that was already created.
In this form I use drop down menus for the dates and times. This selects the month, day, year and then the hour, min, and either am or pm. I am pulling the preselected values from the db and would like to display them in the same way they were originally selected. Below I have pasted the code to do this, but I don't know how to break down the data from the database into the separate parts. One more thing, when storing it in the database, building the string containing the date is easy enough but for the time should I create a string with a colon separating the hour and minute then a space and either am or pm, or will that not work.

Here is the code for displaying it:
[code]
<tr>
<th>Event Time</th>
<td>
<select name="hour" size="1">
  <option value="<?php echo '$hour'; ?>"> <?php echo '$hour'; ?></option>
  <?php
for($i = 1; $i < 13; $i++)
{
echo '<option value="' . $i . '">';
echo $i;
echo '</option>';
}
  ?>
    </select>
<select name="minute">
  <option value="<?php echo '$min'; ?>"> <?php echo '$min'; ?></option>
  <?php
for($i = 1; $i < 60; $i++)
{
echo '<option value="' . $i . '">';
echo $i;
echo '</option>';
}
  ?>
    </select>
<select name="ampm">
  <option value="<?php echo '$a'; ?>"> <?php echo '$a'; ?></option>
  <option value="am">am</option>
  <option value="pm">pm</option>
    </select>
</td>
</tr>
<tr>
  <th>Date</th>
  <td>
<select name="month">
  <option selected="<?php echo '$month'; ?>"> <?php echo '$month'; ?></option>
  <?php
for($i = 1; $i < 13; $i++)
{
echo '<option value="' . $i . '">';
echo $i;
echo '</option>';
}
  ?>
</select>
<select name="day">
  <option selected="<?php echo '$day'; ?>"> <?php echo '$day'; ?></option>
  <?php
for($i = 1; $i < 32; $i++)
{
echo '<option value="' . $i . '">';
echo $i;
echo '</option>';
}
  ?>
</select>
<select name="year">
  <option selected="<?php echo '$year'; ?>"> <?php echo '$year'; ?></option> 
  <?php
for($i=date(Y); $i < date(Y)+3; $i++)
{
echo '<option value="' . $i . '">';
echo $i;
echo '</option>';
}
  ?>
</select>
  </td>
  <td>&nbsp;</td>
</tr>
[/code]


Any suggestions?
Before I end this I just want to thank everyone on this board, especially those that have been replying to all my many questions over the last week. You have been immeasurably helpful. Thank you!

Thanks,
Patrick
First of all you should add this line to each loop (example for hours):
[code]
if ($db_hour == $i) echo "selected=\"selected=\"";
[/code]

Regarding the values in your database,
The way you're using the dates here is a bit quirky but I don't know, just use an underscore.
Maybe I should elaborate a bit more, in the database I am storing the dates in YYYY/MM/DD format. I will most likely store the time in HH:mm AM/PM format. Now that I am less tired I think I can store the date as a string and use the explode function to separate it into Year, Month, and Day values. But for the Time since I am going to store it with both a colon and a space separating the parts I don't think I can use this function. I know I can store the time in military style 24 hour style, but how do I convert back and forth between the regular 12 hour style with am and pm indicators and 24 hour style? Any ideas?

Thanks,
Patrick
You can store it as "HH:mm AM/PM", then-
[code]<?php

$str="HH:mm AM/PM";
$result = array();
$temp = explode(":",$str);
$result['h'] = $temp['0'];
$temp = explode(" ", $temp['1']);
$result['m'] = temp['0'];
$result['ampm'] = $temp['1'];

print_r($result);

?>[/code]

Orio.
If you're storing your dates as YYYY/MM/DD
then you can do this

[code]$fromDatabase = "2006/11/13";
//Convert it to UNIX timestamp
$toUNIX = strtotime($fromDatabase);
//Convert it to your desired date format using the date() function


//Military Time
$newDate = date("H:i A", $toUNIX);
//12-hour Time
$newDate = date("h:i A", $toUNIX);[/code]
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.