Jump to content

[SOLVED] changing a timestamp into a dropdown menu x3


PC Nerd

Recommended Posts

Hi,

 

ive got a timestamp: $Start_Date  and what I want to do is create drop down boxes for day, hour and minute - and have the timestamps value selected across the three menus.

 

eg, if the timestamp is for 25/12/07 12:30 pm (AEST), then I want to create a drop down menu that has all the days between strtotime("today AEST");  and +2 months, a drop down with the hours 00 - 23, and the minutes 00 - 59.

 

If this doesnt make sense then please let me know.

 

If anyone has any suggestions, I'd really appreciate them.  I also need it to work with different timezones - and nto work off GMT.  ie, I specified timezone in the strtotime function.

 

Thanks for your time

Link to comment
Share on other sites

I don't think this is what you're trying to do, but it should help a little....

 

<?php

$current_day = date('j');
$current_hour = date('G');
$current_minute = (int) date('i');
$current_second = (int) date('s');

//none of the dates have leading 0s, because if they did, they would be worse for comparing to $i

echo 'Day: <select name="days">';
for($i = 1; $i <= 31; $i++) {
$s = ($current_day == $i) ? ' SELECTED' : '';
echo "\t<option value=\"{$i}\"{$s}>{$i}</option>";
}
echo '</select>';

echo '  Hour: <select name="hour">';
for($i = 0; $i < 24; $i++) {
$hr = $i;
if($hr < 10) (string) $hr = '0'.$i;
$s = ($current_hour = $i) ? ' SELECTED' : '';
echo "\t<option value=\"{$i}\">{$hr}</option>";
}
echo '</select>';

?>

 

And yeah, I didn't finish it, mainly because I'm lazy lol.

Link to comment
Share on other sites

ok.

 

that helps a little.

 

a) how woud li change that to select the timestamp placed into the code?  ie i can generate a form with the selected values into the three arrays, however i cannot see how to select teh current values.

 

also b)  Im not familiar with teh shorthand if statement ( i recognise it not dont know how to read it.)

 

 

Thsi is my code for the three menues:

 

<?php
$Days = array();
$Hours = array();
$Minutes = array();

$follow = 1;
for($i=strtotime("today AEST");$follow == 1;$i += 86400) {

$day = date("d/m/y - l", $i);
$Days[$i] = $day;
#$i = $i + 60*60*24;
if($i >= strtotime("+ 2 months")) {$follow = 0;}
}

for($i = 0; $i < 24;$i++) {
$hour = $i * 60 * 60;
$Hours[$i] = $hour;
}

for ($i = 0; $i < 60; $i++) {
$Minutes[$i] = $i*60;
}
?>

 

from there i just loop through and add them to the menus.

 


Start Date: <select name = 'Start_date_day'><?php foreach($Days as $field => $value) { echo "<option value = '".$field."'>".$value."</option>\n";} ?></select>

      Hour: <select name = 'Start_date_hour'><?php foreach($Hours as $field => $value) { echo "\t<option value = '".$value."'>".$field."</option>\n";} ?></select>

      Minutes: <select name = 'Start_date_minute'><?php foreach($Minutes as $field => $value) { echo "\t<option value = '".$value."'>".$field."</option>\n";} ?></select>

 

Thanks for your help so far

 

 

 

EDIT:

 

I wanted to be able to create the menus the same way (same code as posted), but have a way of checking to see if its part of the current date etc.  and select it etc.

 

thnks

Link to comment
Share on other sites

So I'm not sure what you're trying to do....

 

Is this correct:

 

You want to make 3 drop down menus.  The first one has days in the form of 00, 01, 02, 03, 04, 05, 06 so on up until 2 months from now (90ish)?  The second is hours with 00, 01, 02, 03 up until 23, and then the last is minutes from 00 to 60?  And then these drop down boxes have selected values based on today's date?

Link to comment
Share on other sites

um basically yes.

 

however the day will be in teh format of:

17/12/07 - Monday etc.

 

 

 

here is the output of my code i posted in my second post.

this is only a few lines of it becasue it echos out to be hundreds of lines

 

FOR THE DAYS:

 

<option value = '1202083200'>04/02/08 - Monday</option>

<option value = '1202169600'>05/02/08 - Tuesday</option>

<option value = '1202256000'>06/02/08 - Wednesday</option>

<option value = '1202342400'>07/02/08 - Thursday</option>

<option value = '1202428800'>08/02/08 - Friday</option>

<option value = '1202515200'>09/02/08 - Saturday</option>

<option value = '1202601600'>10/02/08 - Sunday</option>

<option value = '1202688000'>11/02/08 - Monday</option>

<option value = '1202774400'>12/02/08 - Tuesday</option>

<option value = '1202860800'>13/02/08 - Wednesday</option>

<option value = '1202947200'>14/02/08 - Thursday</option>

<option value = '1203033600'>15/02/08 - Friday</option>

<option value = '1203120000'>16/02/08 - Saturday</option>

<option value = '1203206400'>17/02/08 - Sunday</option>

 

 

FOR THE HOURS

 

<option value = '3600'>1</option>

<option value = '7200'>2</option>

<option value = '10800'>3</option>

<option value = '14400'>4</option>

<option value = '18000'>5</option>

<option value = '21600'>6</option>

<option value = '25200'>7</option>

<option value = '28800'>8</option>

<option value = '32400'>9</option>

 

 

FOR THE MINUTES:

 

<option value = '60'>1</option>

 

<option value = '120'>2</option>

<option value = '180'>3</option>

<option value = '240'>4</option>

<option value = '300'>5</option>

<option value = '360'>6</option>

<option value = '420'>7</option>

 

<option value = '480'>8</option>

<option value = '540'>9</option>

<option value = '600'>10</option>

 

 

 

those are my values echoed out for my drop down menu's.

 

I want to simply select the valid ones.

thanks

 

 

Link to comment
Share on other sites

firslty that HTML example- is the output from the code i pasted above.

 

secondly - its only a snippet of the output - each line is teh same except its values.

 

finally i want whatever item to be selected, that would combine to give me

 

$Start_Date, which is a timestamp from a database ( which i know is within the values of this form.)

 

eg, if the time was XXX day XXX Hours, 10 minutes.  the <option value = '600'>10</option> would become <option value = '600' selected>10</option>

 

does thsi make sense?

 

what i need is a function that given the arrays of values ( which is $Days $Hours and $Minutes from the code i pasted above) - i need it to tell me what the value os $Start_Date is - through those drop down menus

 

im stumped on how to do this

 

 

Thanks

Link to comment
Share on other sites

OK....  I don't think this is exactly what you're aiming for, and this would obviously require parsing the DB input since you can see where I set the values by hand, but here it is:

 

$in2months = time() + (86400*62);

$days = 5;
$hours = 22;
$minutes = 32;

echo '<select name="days">';
$day = 0;
for($i = 0; $i <= 7776000; $i += 86400) {
$value = date('m/d/y - l', $in2months + $i);
$s = ($day == $days) ? ' SELECTED' : '';
echo "\t<option value=\"{$i}\"{$s}>{$value}</option>\r\n";
$day++;
}
echo '</select>';

echo '<select name="hours">';
$hour = 0;
for($i = 0; $i < 86400; $i += 3600) {
(string) $hr = ($hour < 10) ? '0' . $hour : $hour;
$s = ($hour == $hours) ? ' SELECTED' : '';
echo "\t<option value=\"{$i}\"{$s}>{$hr}</option>\r\n";
$hour++;
}
echo '</select>';

echo '<select name="hours">';
$minute = 0;
for($i = 0; $i < 3600; $i+= 60) {
(string) $min = ($minute < 10) ? '0' . $minute : $minute;
$s = ($minute == $minutes) ? ' SELECTED' : '';
echo "\t<option value=\"{$i}\"{$s}>{$min}</option>\r\n";
$minute++;
}
echo '</select>';

Link to comment
Share on other sites

ok i tried that.

 

and this is my exact timestamp  - its an example of one out of the database

 

1197908318

 

from here, if i could simply determine the minutes, hours and date i could get the selction working.

 

also - as i thin ki mentioned before - i need this to all work off a certain timezone, which meants i htink well need to user strtotime() to get the times... eg strtotime( "+ 2 months AEST"); etc.

 

Thanks

Link to comment
Share on other sites

From what I understand, timestamps (at least returned by time()) are UTC....

 

Anyway, that means this should work:

 

Here's an example of getting the day/hour/minute of a timestamp with different time zones (my localtime is GMT -6):

 

<?php

$timestamp = 1197908318;
list($days, $hours, $minutes) = explode('|', gmdate('j|G|i', $timestamp));
echo "GMT:\r\n";
echo $days . "\r\n";
echo $hours . "\r\n";
echo $minutes . "\r\n";

echo "\r\nLocaltime (GMT -6):\r\n";

list($days, $hours, $minutes) = explode('|', date('j|G|i', $timestamp));
echo $days . "\r\n";
echo $hours . "\r\n";
echo $minutes . "\r\n";


$offset = 2;

$timestamp = 1197908318;
list($days, $hours, $minutes) = explode('|', gmdate('j|G|i', $timestamp+$offset*3600));
echo "\r\nGMT +{$offset}:\r\n";
echo $days . "\r\n";
echo $hours . "\r\n";
echo $minutes . "\r\n";

?>

 

Output:

 

C:\Users\Corbin\Desktop>php i.php

GMT:

17

16

18

 

Localtime (GMT -6):

17

10

18

 

GMT +2:

17

18

18

 

 

So theoretically, using my script from earlier, you could do:

 

$in2months = 1197908318;
//yeah, the varname doesn't exactly make sense anymore, but I'm too lazy to change it ;p.
list($days, $hours, $minutes) = explode('|', gmdate('j|G|i', $in2months));
//This script would show the stuff as GMT because of gmdate....  You could easily make it $in2months - <timezone>*3600 to have a timezone besides the server timezone....  Or you could use date_default_timezone or something similar.


echo '<select name="days">';
$day = 0;
for($i = 0; $i <= 7776000; $i += 86400) {
$value = date('m/d/y - l', $in2months + $i);
$s = ($day == $days) ? ' SELECTED' : '';
echo "\t<option value=\"{$i}\"{$s}>{$value}</option>\r\n";
$day++;
}
echo '</select>';

echo '<select name="hours">';
$hour = 0;
for($i = 0; $i < 86400; $i += 3600) {
(string) $hr = ($hour < 10) ? '0' . $hour : $hour;
$s = ($hour == $hours) ? ' SELECTED' : '';
echo "\t<option value=\"{$i}\"{$s}>{$hr}</option>\r\n";
$hour++;
}
echo '</select>';

echo '<select name="hours">';
$minute = 0;
for($i = 0; $i < 3600; $i+= 60) {
(string) $min = ($minute < 10) ? '0' . $minute : $minute;
$s = ($minute == $minutes) ? ' SELECTED' : '';
echo "\t<option value=\"{$i}\"{$s}>{$min}</option>\r\n";
$minute++;
}
echo '</select>';

Link to comment
Share on other sites

Oh sorry!  I forgot about that!

 

It's called the ternary operator....

 

It basically goes

 

(condition) ? true action : false action;

 

So, that would be short for:

 

if(condition) {

    true action;

}

else {

    false action;

}

 

 

Oh and with:

 

$var = (condition) ? true act : false act;

 

That would be like:

 

if(condition) {

    $var = true act;

}

else {

    $var = false act;

}

Link to comment
Share on other sites

ok - got it working at last.

 

here it is for reference

 

 

### CREATE TEH TIME ARRAYS

 

$Days = array();
$Hours = array();
$Minutes = array();

#$follow = 1;
#for($i=time();$follow == 1;$i += strtotime("+ 1 day", $i)) {
$i = time();
while($i <= strtotime("+ 2 months")) {

$day = date("d/m/y - l", $i);
echo "".$i."<br>";
$Days[$i] = $day;
$i = $i + 60*60*24;
#if($i >= strtotime("now + 2 months AEST")) {$follow = 0;}
}

 

 

 

###CREATE THE MENU

 

$Start_Date = $ROW['Start_Date'];

echo date("d/m/y", $Start_Date)."<br>";

echo "<form action = '***' method = 'POST'>\n";

echo "Start Date: <select name = 'Start_date_day'>\n";

foreach($Days as $field => $value) {
if(date("d/y", $Start_Date) == date("d/y", $field)) {$s = " selected";}
else {$s = "";}
#echo date("d/m/y", $field)." => ".date("d/m/y", $Start_Date)."<br>";
echo "<option value = '".$field."' ".$s.">".$value."</option>\n";
} 
?>

</select>

 

 

Thanks for all your help.

 

;D ;D ;D:D :D ;) ;) ;D ;D ;D

 

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.