I am trying to return twitter titles based on today's date only. I have made the following code below, but it returns every title no matter if its today's date or not.
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=google');
$xml = simplexml_import_dom($dom);
$twitter = $xml->xpath("//item");
foreach ($twitter as $item) {
$timezone = new DateTimeZone('America/Los_Angeles');
$date = new DateTime($item->pubdate);
$date->setTimeZone($timezone);
$twitter_date = $date->format("F j Y");
$todays_date = date("F j Y");
if ($twitter_date == $todays_date) {
foreach ($twitter as $item) {
$text = $item->title;
echo $text.'<br />';
}
}
}
You are looping again through EVERY $twitter inside the if statement. Try removing the foreach tag inside and just using the current $item:
if ($twitter_date == $todays_date) {
$text = $item->title;
echo $text.'<br />';
}
Related
I am working on a small script where I wanna take only my website's latest post which is posted yesterday mean I wanna get all yesterday's links and titles.
I tried with my script but I am getting all URLs I am not sure how can I fix it.
Can anyone help me solve this problem?
I was wondering if I can use 'where' attribute like we usually use in SQL. I want only 1 days posts to be scraped.
<?php
header('Content-Type: application/json');
$url = "https://www.lifegoals.co.in/feed/";
$i=0;
$invalidurl = false;
if(#simplexml_load_file($url)){
$feeds = simplexml_load_file($url);
}else{
$invalidurl = true;
echo "<h2>Invalid RSS feed URL.</h2>";
}
if(!empty($feeds)){
//$site = $feeds->channel->title;
//$sitelink = $feeds->channel->link;
//echo "<h1>".$site."</h1>";
foreach ($feeds->channel->item as $item) {
$title = $item->title;
$link = $item->link;
//$description = $item->description;
$postDate = $item->pubDate;
$pubDate = date('D, d M Y',strtotime($postDate));
$currDate = date('D, d M Y');
if($i>=10) break;
if($pubDate=$currDate){
$rss = "<item>
<title>$title</title>
<link>$link</link>
</item>";
echo $rss;
$i++;
}
}
}
?>
i want only 1 days posts there are 4 days posts
I'd add some debugging to this to ensure that you're getting what you think you want. Try the following in your foreach loop:
print_r([
$postDate,
$pubDate,
$currDate,
($pubDate == $currDate),
]);
if($pubDate==$currDate){
$rss = "<item>
<title>$title</title>
<link>$link</link>
</item>";
echo $rss;
$i++;
}
The == was missing thanks.
I have two strings (read from an XML file), say '2014-10-17T11:17:44' and '2014-10-17T16:19:15'. I want to compare them using only the date part (without time). I already tried ...script below
if(file_exists($xmlFullFilename)) {
if($entryTimeNode->format("Y-m-d") = $entryTimeNode->format("Y-m-d")){
( appends to the exisiting file ....)
$xmlDoc = new DomDocument();
$tmp = split(" ", $entryTime);
$dateString = $tmp[0] . "T" . $tmp[1];
$entryTimeNode = $xmlDoc->createElement("EntryTime", $dateString);
}
else {
//create a new xml file .
}}
XML FILE
<Incidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Count="2" Date="2014-10-24" Time="12:05:39" FileName="2014-10-24_Brook_Retail_Park.xml">
<Incident>
<EntryTime>2014-10-17T11:17:44</EntryTime>
</Incident>
<Incident>
<EntryTime>2014-10-17T16:19:15</EntryTime>
</Incident></Incidents>
My soluction is
$date = "2014-10-17T11:17:44";
$date2 = "2014-10-17T16:19:15";
$date = new DateTime($date);
$date2 = new DateTime($date2);
$diff = $date->diff($date2);
if((int)$diff->format('%y%m%d')==0)
{
// Dates are equal
}
I have a simple newsfeed which is pulling from an xml file which works great. The news feed is broken down into separate XML files for each year. I want the page to show only one year at a time with links to other years above. Ideally when a new year is clicked I would like the content below to update without going to a new page.
I am trying to avoid repeating the PHP code which is parsing the XML for each year and instead would like to simply update the $dom_object->load source when the user clicks a different year.
I am a PHP newbie so some help would be appreciated!
<?php
$dom_object = new DOMDocument();
$dom_object->load("http://EXAMPLE.com/XML-Feed-10100524539?year=2013");
$item = $dom_object->getElementsByTagName("item");
foreach( $item as $value )
{
$titles = $value->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$pubDates = $value->getElementsByTagName("pubDate");
$pubDate = $pubDates->item(0)->nodeValue;
$pdf_urls = $value->getElementsByTagName("pdf_url");
$pdf_url = $pdf_urls->item(0)->nodeValue;
//Trims after last space - REMOVES EST
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
//Trims after remaining space - REMOVES TIME
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
$pubDater = str_replace('/', '-', $pubDate);
$newDate = DateTime::createFromFormat("m/d/Y", $pubDate);
$newDate = $newDate->format('F d, Y');
echo "<div style=\"width:33%; float:left; display:inline-block; height:150px;\"><div style=\"padding:10px;\"><h4>$newDate</h4><p>$title</p></div></div>";
}
?>
You would need to use ajax by using $.get or $.post so you could do something like:
$('#yourElement').click(function(){
var path = $('#path'); // for example
$.post("your/path/getXML.php", { path: path },
function (dat) {
console.log(dat);
}
);
});
Where getXML would be a file similar to this one:
$dom_object = new DOMDocument();
$dom_object->load($_POST["path"]);
$item = $dom_object->getElementsByTagName("item");
foreach( $item as $value )
{
$titles = $value->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$pubDates = $value->getElementsByTagName("pubDate");
$pubDate = $pubDates->item(0)->nodeValue;
$pdf_urls = $value->getElementsByTagName("pdf_url");
$pdf_url = $pdf_urls->item(0)->nodeValue;
//Trims after last space - REMOVES EST
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
//Trims after remaining space - REMOVES TIME
$pubDate=substr($pubDate, 0, strrpos($pubDate, ' '));
$pubDater = str_replace('/', '-', $pubDate);
$newDate = DateTime::createFromFormat("m/d/Y", $pubDate);
$newDate = $newDate->format('F d, Y');
echo "<div style=\"width:33%; float:left; display:inline-block; height:150px;\"><div style=\"padding:10px;\"><h4>$newDate</h4><p>$title</p></div></div>";
I am trying to parse a Google Calendar to use on our TV's to display 'Today's Events'.
While I am most of the way there thanks to the help of a friend, I wanted to see if somebody could help me the rest of the way.
The code below generates the calendar with all the information, but for EVERY entry it shows the date. Since they are all the same day, this is kind of frustrating and confusing when looking at it. I am nowhere near a programmer, but I can make sense of some things.
How would I group all Todays events under a single date heading?
Thanks in advance.
<?php
$confirmed = 'http://schemas.google.com/g/2005#event.confirmed';
$three_months_in_seconds = 60 * 60 * 24 * 28 * 3;
$three_months_ago = date("Y-m-d\Th:i:s", time() - $three_months_in_seconds);
$three_months_from_today = date("Y-m-d\Th:i:s", time() + $three_months_in_seconds);
$params = "?orderby=starttime&start-min=" . $three_months_ago . "&start-max=" . $three_months_from_today;
//$params = "?orderby=starttime&start-min=2012-12-01T05:48:47&start-max=2013-05-07T05:48:47&sortorder=a&singleevents=true&futureevents=true";
$params = "?orderby=starttime&sortorder=a&singleevents=true&futureevents=true";
$feed = "https://www.google.com/calendar/feeds/REDACTED%40gmail.com/private-REDACTED/full".$params;
$doc = new DOMDocument();
if (!$doc->load( $feed )) echo 'failed to load';
$entries = $doc->getElementsByTagName( "entry" );
foreach ( $entries as $entry ) {
$status = $entry->getElementsByTagName( "eventStatus" );
$eventStatus = $status->item(0)->getAttributeNode("value")->value;
if ($eventStatus == $confirmed) {
$titles = $entry->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
$times = $entry->getElementsByTagName( "when" );
$startTime = $times->item(0)->getAttributeNode("startTime")->value;
$when = date( "D M j, Y", strtotime( $startTime ) );
$time = date("g:i A",strtotime($startTime));
$places = $entry->getElementsByTagName( "where" );
$where = $places->item(0)->getAttributeNode("valueString")->value;
print "<div class='row when'>$when</div>";
echo "<div class='row event'><span class='time'>$time</span><span class='title'>$title</span><span class='where'>$where</span></div>";
// print $where . "\n";
print "\n";
}
}
?>
Have an answer:
just change this:
print "<div class='row when'>$when</div>";
to this:
if ($old_when!=$when) print "<div class='row when'>$when</div>"; $old_when=$when;
and add
$old_when = null;
before the foreach
I have script which scrap/fetch html table data from another website. The website date format is 26/8/2011, How I can change it to this format 2011-12-13??
function createRSSFile($tag,$value,$data)
{
# this will return the each element with tag.
$tag=strtolower(str_replace(" ","_",$tag));
$tag=strtolower(str_replace(":","",$tag));
$tag=strtolower(str_replace("&","and",$tag));
$returnITEM = "<".$tag.">".htmlspecialchars(str_replace(" 00:00:00","",$value))."</".$tag.">";
return $returnITEM;
}
function fetchData($jobid) {
$html=file_get_contents('http://acbar.org/JobDetail.aspx?id='.$jobid);
$html=str_replace("<td></td>", "",$html);
$html=str_replace("<td style=\"font-size:8pt;font-weight:bold;\"></td>","<td style=\"font-size:8pt;font-weight:bold;\">Null</td>",$html);
$html=str_replace("<td style=\"font-size:8pt;font-weight:bold;\" colspan=\"2\" ></td>","<td style=\"font-size:8pt;font-weight:bold;\" colspan=\"2\" >Null</td>",$html);
$html=str_replace(" ", " ",$html);
$html=str_replace("?", "<br>",$html);
$html=str_replace("<br>", "_br_",$html);
$dom = new DOMDocument;
$dom->loadHTML( $html );
//echo $dom->saveHTML();
//exit;
$rows = array();
foreach( $dom->getElementsByTagName( 'tr' ) as $tr ) {
$cells = array();
foreach( $tr->getElementsByTagName( 'td' ) as $td ) {
if(trim($td->nodeValue)!='')
$cells[] = str_replace("_br_","<br>",trim($td->nodeValue));
}
if(sizeof($cells)>0)
$rows[] = $cells;
}
Could always strtotime the date passed, then format it however you wish with date, like so...
$timeToModify = strtotime($passedTime);
$formattedTime = date("Y-m-D", $timeToModify);