Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-22-2012, 11:58 PM   PM User | #1
nickburrett
New Coder

 
Join Date: Jun 2009
Posts: 64
Thanks: 22
Thanked 0 Times in 0 Posts
nickburrett is an unknown quantity at this point
PHP Array?

I am trying to combine several variables into one PHP array (I think)

Basically the user keys in the date and time into 6 boxes:

YYYY MM DD HH MM SS

I am then trying to combine those fields into one array.

Whatever I'm doing is not working and I'd really appreciate some help.

PHP Code:
$start_time $start_year,$start_month,$start_date,$start_hour,$start_minute,$start_seconds;
echo 
$start_time
nickburrett is offline   Reply With Quote
Old 10-23-2012, 12:11 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Looks to me that you mean a string, not an array.
To convert that to an array, you'd simply wrap it in array(). So that would be array($start_year, . . ., $start_seconds);.

What you want is a string. I'd recommend a sprintf as its simple.
PHP Code:
$start_time sprintf('%d %d %d %d %d %d'$start_year$start_month$start_date$start_hour$start_minute$start_seconds); 
Since these come from HTML, you can also generate the array there. That may be a lot easier to work with and be more beneficial overall.
Code:
<input type="text" name="inputDate[year]" />
<input type="text" name="inputDate[month]" />
...
Then it will be retrieved as an array already. This is nice as to match the format you have above you'd simply implode() with a space, but you can still look up what you need based on the offset of $_POST['inputDate']['itemhere'].

Edit:
BTW, if you split up the date from the time strings, you can parse it easily using strtotime or new DateTime objects. This makes it easier to deal with other calculations based on time as well as letting you format it however you want.
PHP Code:
// input types date and time
$sDateStr implode(''$_POST['inputDate']);
$sTimeStr implode('.'$_POST['inputTime']);
$dt = new DateTime("$sDateStr $sTimeStr");
print 
$dt->format('Y-m-d H:i:s'); 
For example. strtotime() and date() can be used in place of datetime objects.

Last edited by Fou-Lu; 10-23-2012 at 12:17 AM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
nickburrett (10-25-2012)
Reply

Bookmarks

Tags
array, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:32 PM.


Advertisement
Log in to turn off these ads.