Stumped with strtotime()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

Stumped with strtotime()

Post by WorldCom »

Hey all, this one is stumping me good.
I am separating the month, day and year for use in a drop down menu with the strtotime() function.
There are two dates I'm using this on. The first one works no problem and I have NO idea why the second will not pick up these $_POST values.

Code: Select all

<?php
	$query = mysql_query("SELECT products.registration_date, products.renewal_date, products.price, catagories.name, subCatagories.name, items.name, items.model_num, items.price FROM products, catagories, subCatagories, items WHERE products.id=$product_id AND products.product_id=items.items_id AND subCatagories.subcat_id=items.subcat_id AND catagories.cat_id=subCatagories.cat_id");
	$client_product = mysql_fetch_array($query) or die ('Database Error: ' . mysql_error());
	$_POST['month'] = date('n', strtotime($client_product[0]));
	$_POST['day'] = date('j', strtotime($client_product[0]));
	$_POST['year'] = date('Y', strtotime($client_product[0]));
	$_POST['month1'] = date('n', strtotime($client_product[1]));
	$_POST['day1'] = date('j', strtotime($client_product[1]));
	$_POST['year1'] = date('Y', strtotime($client_product[1]));
?>

<form action="" method="post">
<table width="440" class="form" style="margin:auto;">
<tr><td colspan="2" class="fieldarea" style="text-align:center;"><input type="hidden" name="id" value="<?php echo $product_id; ?>" /><b>Client Products</b></td></tr>
<?php
if( $error_code > 0 ){
	echo "<tr align='center'><td colspan='2' class='". $tbl_class[$error_code] ."'>$user_message</td></tr>";
}
?>
<tr align="center"><td colspan="2"><?php echo $client_product[3]; ?> - <?php echo $client_product[4]; ?></td></tr>
<tr><td align="right">Registration Date:</td><td>
<?php
echo "<SELECT NAME='month'>";
foreach( $months AS $key => $value ){
	echo "<OPTION VALUE='$key'";
	if( $_POST['month'] == $key ) { echo " SELECTED"; }
	echo ">$value</OPTION>";
	}
echo "</select>";
echo "<SELECT NAME='day'>";
for( $days=1; $days<=31; $days++ ){
	echo "<OPTION VALUE='$days'";
	if( $_POST['day'] == $days ) { echo " SELECTED"; }
	echo ">$days</OPTION>";
	}
echo "</select>";
?>
<INPUT TYPE="text" maxlength="4" NAME="year" VALUE="<?php echo $_POST['year']; ?>" style="width:4em; text-align:center; font-weight:bold;"></td></tr>

<tr><td align="right">Renewal Date:</td><td>
<?php
echo "<SELECT NAME='month1'>";
foreach( $months AS $key => $value ){
	echo "<OPTION VALUE='$key'";
	if( $_POST['month1'] == $key ) { echo " SELECTED"; }
	echo ">$value</OPTION>";
	}
echo "</select>";
echo "<SELECT NAME='day1'>";
for( $days=1; $days<=31; $days++ ){
	echo "<OPTION VALUE='$days'";
	if( $_POST['day1'] == $days ) { echo " SELECTED"; }
	echo ">$days</OPTION>";
	}
echo "</select>";
?>
<INPUT TYPE="text" maxlength="4" NAME="year1" VALUE="<?php echo $_POST['year1']; ?>" style="width:4em; text-align:center; font-weight:bold;"></td></tr>
<tr><td align="right">Renewal Date:</td><td><INPUT TYPE="text" NAME="test" VALUE="<?php echo $client_product[1]; ?>" style="width:14em; text-align:center; font-weight:bold;"></td></tr>

<tr align="center"><td colspan="2"><p class="submit"><input type="submit" name="update_details" value="Update Client Product Details" /></p><p class="submit"><input type="submit" name="delete_product" value="Delete Product" /></p></td></tr>
</table>
</form>
The drop down for month, day, year from the variable strtotime value from $client_product[0] works fine. The EXACT same drop down with $client_product[1] only gives the dreaded Dec 31 1969.
Ok, so I thought there was something wrong with $client_product[1] value. I just put it into a text box and it prints 2100-02-03, just what is in the database.

Just for fun, to check my own syntax above I changed to $client_product[0] to $client_product[1] and $client_product[1] to $client_product[0]. This puts the data in the wrong places, but it again only works on the $client_product[0] value.

Any ideas would be great.
dukesdemise
Forum Newbie
Posts: 6
Joined: Tue Jun 01, 2010 11:16 am

Re: Stumped with strtotime()

Post by dukesdemise »

Hi Worldcom,

Besides giving warnings, the following code yields the "dreaded" date you mention. You have probably thought of this, but perhaps the values you are retrieving from your database are NULL values. For example:

Code: Select all

<?php
$month = date('n', strtotime(NULL));
$day = date('j', strtotime(NULL));
$year = date('Y', strtotime(NULL));
echo "month [$month]".PHP_EOL;
echo "day [$day]".PHP_EOL;
echo "year [$year]".PHP_EOL;
?>
yields:

month [12]
day [31]
year [1969]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Stumped with strtotime()

Post by Weirdan »

it prints 2100-02-03
2100 ? is that right?
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

Re: Stumped with strtotime()

Post by WorldCom »

Weirdan wrote:
it prints 2100-02-03
2100 ? is that right?
Yes it is lol
I just put that in the database to verify that it picks the data up. So, the variable $client_product[1] is '2100-02-03', I just don't know why that strtotime() doesn't seem to extract the info.

I have also tried to reverse the SELECT products.renewal_date, products.registration_date . . .
The renewal_date parses fine and the registration date gives the bad date.
Last edited by WorldCom on Sun Feb 13, 2011 4:40 am, edited 1 time in total.
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

Re: Stumped with strtotime()

Post by WorldCom »

dukesdemise wrote:Hi Worldcom,

Besides giving warnings, the following code yields the "dreaded" date you mention. You have probably thought of this, but perhaps the values you are retrieving from your database are NULL values. For example:

Code: Select all

<?php
$month = date('n', strtotime(NULL));
$day = date('j', strtotime(NULL));
$year = date('Y', strtotime(NULL));
echo "month [$month]".PHP_EOL;
echo "day [$day]".PHP_EOL;
echo "year [$year]".PHP_EOL;
?>
yields:

month [12]
day [31]
year [1969]
Thanks for the info dukesdemise, but that is something I already know. When you get that date, something just ain't right ;)
That is why I echoed out the varibable $client_product[1] and it is a properly formatted date.

Still stumped ???
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Stumped with strtotime()

Post by Weirdan »

strtotime() converts date to an integer, therefore it may be limited to the integer range, which on 32bit systems ends somewhere in 2038.

Use DateTime instead - it uses 64bit internally and has a much wider range - something like +/- 290 billion years from now.
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

Re: Stumped with strtotime()

Post by WorldCom »

Weirdan wrote:strtotime() converts date to an integer, therefore it may be limited to the integer range, which on 32bit systems ends somewhere in 2038.

Use DateTime instead - it uses 64bit internally and has a much wider range - something like +/- 290 billion years from now.
As soon as you said that, it clicked. Stupid me .......... thanks for all the help :)
BTW, it was working all the time. :roll:
Post Reply