Page 1 of 1

Date

Posted: Fri Jan 30, 2004 1:37 am
by glennalmeda
How can i make the variables $Year (e.g. 2004), $Month (e.g. January) , and $Day (e.g. 30) stored in a single variable in a date format?

thank you :)

Posted: Fri Jan 30, 2004 1:44 am
by John Cartwright
It all depends on how you want to store it. There are many ways you can go about this....

You can store it into a mysql db for instance...

then you can take taht variable, set it in an array then explode it to get each seperate part of the date it's own variable... be more specific plz

Posted: Fri Jan 30, 2004 1:52 am
by John Cartwright
I recently made a script that does that, it takes in a date ( yyyy-mm-dd ) and then it explodes it to $year $month $day

Code: Select all

<?php
echo "<div align="center">"
	."<form action="{$_SERVER['PHP_SELF']}"\n"
	."<p>Type your Date here: eg. 2004-01-31 (y-m-d)<br/>\n" 
	."<input type="text" name="date" cols="40"><br>\n"
	."<input type="submit" name="submitdate" value="SUBMIT" /></p>\n" 
	."</form>\n"

	$newdate = explode("-",$date); 
	$month = $newdate[1]; // eg. February 
                $day = $newdate[2]; // eg. 04 
                $year = $newdate[0]; // eg. 2004

echo "the current day is $day <br>\n"
       ."the current month is $month <br>\n"
       ."the current year is $year <br>\n";
?>
If you want to do the opposite, such as getting all the different variables to come together into 1 variable date (ie. year+day+month = $date ) then you would probably want to set up an array...

date

Posted: Fri Jan 30, 2004 1:57 am
by glennalmeda
ok. here's my problem...
For example, i have a variables named Month = "January", Day = "30" and Year = "2004".
Those are user-input. I want all of them stored in a single variable for example named Birthday. So how can i combine all those variables and stored in one variable in a date format so i can use it and store in my database. Hope u get my question. thank you

Posted: Fri Jan 30, 2004 1:58 am
by John Cartwright
The code I posted up there is what you are looking for... if you need more help on how to store it properly in your database and then retrieve it properly jus ask

Posted: Fri Jan 30, 2004 2:11 am
by glennalmeda
i think the code u posted above is the other way around.
Your is like trying to parse the content of the variable and get the month, day and year.

My problem is, wat if you are given the month, day, and year... how can i store it in a variable.

Posted: Fri Jan 30, 2004 2:16 am
by John Cartwright
Then you would use the implode function found here - http://ca2.php.net/manual/en/function.implode.php

Code: Select all

<?php

$array = array('year', 'month', 'day');
$date = implode("-", $array);

echo "the current date is $date\n";

?>
That should do the trick.

Posted: Fri Jan 30, 2004 2:19 am
by glennalmeda
Here a sample code:
$frmDate = "$ddFrmYear-$ddFrmMonth-$ddFrmDay";
(e.g. frmDate = 2004-01-1)
$frmDate = "$ddThruYear-$ddThruMonth-$ddThruDay";
(e.g. ThruDate = 2004-02-1)

and i have sql statement like this:
$sql = "SELECT * FROM order_history, dealer_table WHERE o_num = '$txtOrder_Number' AND o_status = '$ddStatus' AND company LIKE '%$txtCompany%' AND o_date BETWEEN '$FrmDate' AND '$ThruDate'";

but i think frmDate n ThruDate is invalid when i use it in sql statement because its not converted into a date format...

Posted: Fri Jan 30, 2004 2:20 am
by John Cartwright
Here a sample code:
$frmDate = "$ddFrmYear-$ddFrmMonth-$ddFrmDay";
(e.g. frmDate = 2004-01-1)
$frmDate = "$ddThruYear-$ddThruMonth-$ddThruDay";
(e.g. ThruDate = 2004-02-1)
You understand you are declaring $frmDate twice, right?

BTW: If you want to be using date format... make sure you are not getting it inputted like this, January, 04 2004 or something. It's gotto be yyyy-mm-dd

Posted: Fri Jan 30, 2004 2:38 am
by John Cartwright
What you want to do instead of all that is do something like this

Code: Select all

<php
$array_frmDate = array('ddFrmYear', 'ddFrmMonth', 'ddFrmDay'); 
$FrmDate = implode("-", $array_frmDate); 

$array_ThruDate = array('ddThruYear', 'ddThruMonth', 'ddThruDay'); 
$ThruDate = implode("-", $array_ThruDate); 
?>


try echo'ing your arrays to see if they are getting the proper input.

eg.

Code: Select all

<?php
echo "$ThruDate";
?>