Copy link to clipboard
Copied
I can't explain this without showing all the code...
I have a script that basically scrapes information off of the myspace website and puts the information into an array. I then had the script put that into a table format. The problem I'm hoping to get help with, I'd like to change the date format, and I'm not sure how I can do this since with the way this is setup to loop through the array. If someone can show me a better way to do this without looping, or a way to convert the date, I'd greatly appreciate it.
06-18-2009 22:00 to Thur June 18, 2009 10:00pm
<?php
include('simplehtmldom/simple_html_dom.php');
$myspace_url = "url I'm scraping";
ini_set('user_agent', 'Scrape/2.5');
$html = file_get_html($myspace_url);
$i = 0;
foreach ($html->find('input[type="hidden"]') as $k => $v) {
if ($v->name == 'calEvtLocation') {
$shows[$i]['Location'] = $v->value;
}
if ($v->name == 'calEvtCity') {
$shows[$i]['City'] = $v->value;
}
if ($v->name == 'calEvtState') {
$shows[$i]['State'] = $v->value;
}
if ($v->name == 'calEvtDateTime') {
$shows[$i]['Date'] = $v->value;
$i++;
}
}
?>
<table border="0" align="center" cellpadding="8" cellspacing="0">
<tr>
<th style="text-align: left; font-size: 12px;">Venue</th>
<th style="text-align: left; font-size: 12px;">City</th>
<th style="text-align: left; font-size: 12px;">State</th>
<th style="text-align: left; font-size: 12px;">Date/Time</th>
</tr>
<?php
foreach($shows as $show)
{
echo '<tr>';
foreach($show as $item)
{
echo "<td>$item</td>";
}
echo '</tr>';
}
?>
</table>
Moving the $i++ shouldn't have any effect on the way the items are subsequently displayed.
The way I would handle the table is like this:
...<table border="0" align="center" cellpadding="8" cellspacing="0">
<tr>
<th style="text-align: left; font-size: 12px;">Venue</th>
<th style="text-align: left; font-size: 12px;">City</th>
<th style="text-align: left; font-size: 12px;">State</th>
<th style="text-align: left; font-size: 12px;">Date/Time</th>
</tr>
<?php
foreach($shows as $show)
{
echo "<tr><td>{$show['Lo
Copy link to clipboard
Copied
To convert date and time from 06-18-2009 22:00 to Thur June 18, 2009 10:00pm, use the function date with required parameters as u want. From above format, u may use date(l F j,Y g:i A) whereby it will returned as Thur June 18, 2009 10:00 PM. You must look through HERE for other's parameters available.
Copy link to clipboard
Copied
I can't get that function to work within the screen scraper script I'm running. Can I have an example that works with
if ($v->name == 'calEvtDateTime') {
$shows[$i]['Date'] = $v->value;
Copy link to clipboard
Copied
Hmm.. I never use the method as u did. May I know how did u store the date and time in database? All the values were in one row in a table?
Copy link to clipboard
Copied
I don't store them. The script scrapes the data from the page and puts it into an array like this:
Array ( [0] => Array ( [Location] => The Social [Title] => band at The Social [City] => Toronto [State] => Ontario [Zip] => [Date] => 06-25-2009 20:00 ) [1] => Array ( [Location] => 12th & Porter [Title] => band at 12th & Porter [City] => Nashville [State] => Tennessee [Zip] => [Date] => 06-26-2009 20:00 )
etc.
and I convert the arrays directly into a table format. That's where I'm lost, in attempting to modify the date.
Copy link to clipboard
Copied
Im not sure enough in how to convert both date and time from an array. But I think you may look at date() and mktime() function to do so.
Copy link to clipboard
Copied
First of all, there's an inherent danger in your loop. The counter is incremented inside the final conditional statement like this:
if ($v->name == 'calEvtDateTime') {
$shows[$i]['Date'] = $v->value;
$i++;
}
}
If, for any reason, the date isn't present in one subarray, the counter will be out of sync. It should be moved out of the final conditional statement, but remain inside the loop:
if ($v->name == 'calEvtDateTime') {
$shows[$i]['Date'] = $v->value;
}
$i++;
}
Presumably, $v->value is the unformatted date. All that's necessary is to create a function to format the date, and pass $v->value to it as an argument.
The following function formats the date as you have specified:
function dp_dateformat($date) {
$val = 'Date not given';
$d = preg_split('/[-\s:]/', $date);
if (is_array($d)) {
if (count($d) == 5) {
$ts = mktime($d[3], $d[4], 0, $d[0], $d[1], $d[2]);
$val = date('D F j, Y g:ia', $ts);
} elseif (count($d) == 3) {
$ts = mktime(0,0,0,$d[0], $d[1], $d[2]);
$val = date('D F j, Y', $ts);
}
}return $val;
}
Use it like this:
if ($v->name == 'calEvtDateTime') {
$shows[$i]['Date'] = dp_dateformat($v->value);
}
Copy link to clipboard
Copied
you never cease to amaze me with your knowledge of php.
one thing.
If i place the $i++; out as suggested, all the results are coming in one cell of the table. How do I need to do this part to get it to be in a table where each value is getting a cell?
<table border="0" align="center" cellpadding="8" cellspacing="0">
<tr>
<th style="text-align: left; font-size: 12px;">Venue</th>
<th style="text-align: left; font-size: 12px;">City</th>
<th style="text-align: left; font-size: 12px;">State</th>
<th style="text-align: left; font-size: 12px;">Date/Time</th>
</tr>
<?php
foreach($shows as $show)
{
echo '<tr>';
foreach($show as $item)
{
echo "<td>$item</td>";
}
echo '</tr>';
}
?>
Copy link to clipboard
Copied
Moving the $i++ shouldn't have any effect on the way the items are subsequently displayed.
The way I would handle the table is like this:
<table border="0" align="center" cellpadding="8" cellspacing="0">
<tr>
<th style="text-align: left; font-size: 12px;">Venue</th>
<th style="text-align: left; font-size: 12px;">City</th>
<th style="text-align: left; font-size: 12px;">State</th>
<th style="text-align: left; font-size: 12px;">Date/Time</th>
</tr>
<?php
foreach($shows as $show)
{
echo "<tr><td>{$show['Location']}</td><td>{$show['City']}</td>
<td>{$show['State']}</td><td>{$show['Date']}</td></tr>";
}
?>
Find more inspiration, events, and resources on the new Adobe Community
Explore Now