Page 1 of 1

Explode date [solved]

Posted: Mon Jun 12, 2006 5:25 am
by technofreak
I have a date in the form of "month/day/year" which is obtained from the form. I initialize the value to a variable, say $activation_date.

Code: Select all

$activation_date = $_POST['activation'];
Now I want to calulcate the date after '$validity' days, where the variable holds the number of days. First i need to make an epoch time stamp of the activation_date using mktime() ,and use strtotime() to calculate the final date value.

Code: Select all

$exploded_date = explode("/", $activation_date);
$epoch_date = mktime(0, 0, 0, $exploded_date[0], $exploded_date[1], $exploded_date[2]);
$ending_day = strtotime("+$validity day", $epoch_date);
What I try to do is, eplode the date using "/" as the delimeter. For example, if the date is 06/12/2006, when i explode it becomes $exploded_date = array('06'.'12',2006'). And using this array, i make a epoch time stamp in the next step and in the last step i make use of the strtotime() to calculate the date after the $valdiity_day.

But, am not getting the output for the explode() function. When i try to echo the variable, its jus ehcoing "Array".

Any better ways to do the sutff ? Where am I going wrong ?

Posted: Mon Jun 12, 2006 6:14 am
by twigletmac
To check the exploded array try using print_r():

Code: Select all

echo '<pre>';
print_r($exploded_date);
echo '</pre>';
Also echo out the $activation_date variable to ensure it is in the form you expect.

Mac

Posted: Mon Jun 12, 2006 8:35 am
by nathanr
and remember make time uses the format Hour, Minute, Second, MONTH, Day, Year, you don't want to get your days and months mixed up!

Posted: Fri Jun 16, 2006 4:31 am
by technofreak
I tried to figure out the bug, and what I found was that the array formed as a result of the explode function is NULL. When I echo the array, it just prints "Array" in the output.

Is there a way to split date in the form of /mm/dd/yyyy into an array of the form date[mm, dd, yyyy] other than my previous explode() technique which is not working out ??

Posted: Fri Jun 16, 2006 4:36 am
by JayBird
technofreak wrote:When I echo the array, it just prints "Array" in the output.
That will always happen, you need to use print_r()

Posted: Fri Jun 16, 2006 4:39 am
by nathanr
the explode will be working.. you can't echo an array you have to:

Code: Select all

print_r($arrayname);

Posted: Fri Jun 16, 2006 5:20 am
by technofreak
Buddies,

My problem is not viewing the array. I have tried using print_r(). What i want to know is, how to split a date like 04/31/2006 into an array like $date, where $date[0], $date[1], $date[2] are '04', '31' and '2006' respectively.

my php code to do this is..

Code: Select all

//obtain the date in the form of mm/dd/yyyy from the form
$activated_date = $_POST['activation'];

// then i explode the date with the "/" as delimeter
$exploded_date = explode("/", $activated_date);

//then i use the individual array elements to make an epoch time stamp and calcilate the due date after a certain number of days, indicated by the $validity

$epoch_date = mktime(0, 0, 0, exploded_date[0], exploded_date[1], exploded_date[2]);

$expiry_date = strtotime("+$validity days", $epoch_date);

$expired = strftime('%m/%d/%y', $expiry_date);
In this code, am not able to achieve getting the day, month and year components of the exploded_date, by exploding the date variable :(

Posted: Fri Jun 16, 2006 5:29 am
by bmcewan
Are you sure your post variable $_POST['activation'] is in the form you expect?

Try echo'ing out the variable.

Code: Select all

//obtain the date in the form of mm/dd/yyyy from the form 
$activated_date = $_POST['activation']; 

echo $activated_date; // debug check to see if we are getting the data we expect

// then i explode the date with the "/" as delimeter 
$exploded_date = explode("/", $activated_date);
The rest of your code will work, it just seems that it is either not being passed the data in the correct format for your script or no data is being passed to it at all.

Posted: Fri Jun 16, 2006 7:49 am
by printf
Whats this?

Code: Select all

$epoch_date = mktime(0, 0, 0, exploded_date[0], exploded_date[1], exploded_date[2]);
Do you not see the problem! array elements missing ($) => exploded_date[#], should be => $exploded_date[#]


pif!

Posted: Fri Jun 16, 2006 9:12 am
by bmcewan
printf wrote:Whats this?

Code: Select all

$epoch_date = mktime(0, 0, 0, exploded_date[0], exploded_date[1], exploded_date[2]);
Do you not see the problem! array elements missing ($) => exploded_date[#], should be => $exploded_date[#]


pif!
Sometimes i cannot believe my own eyes, there's me saying the rest of his code is fine. :oops:

Posted: Fri Jun 16, 2006 11:28 am
by technofreak
printf,

Sorry, I missed that "$" while typing here.. :(

Posted: Sun Jun 18, 2006 10:39 am
by technofreak
Thanks buddies,

My code worked just as it was intended to... thanx a lot for your help! :)