compare date only part from the string - php

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
}

Related

Changing XML source on click using PHP

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>";

Group 'Today's Events' under single Date Header?

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

return rss node based on date

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 />';
}

Extract multiple date format from few string variables in php

I need to extract the date out of a string variable, and the date are formatted in various kind of formats as below:
$date1 = "03/12/2011 (Sat)";
$date2 = "3.12.2011 SAT";
$date3 = "Date: 03/12/2011 "; /* <-- the extra trailing space is intentional */
$date4 = "date:03/12/2011";
$date5 = "date: 03/12/2011";
$date6 = "03/12/2011";
$date7 = "13.12.2011 TUE";
What is the best way to create a PHP function which will work for all the input variables above in extracting the correct date info?
For more info on the DateTime object returned by the function, check the PHP documentation for the DateTime class.
/**
* Parses date from string
* #param string $str Uncorrected date string
* #return DateTime PHP datetime object
*/
function date_grab($str)
{
// regex pattern will match any date formatted dd-mm-yyy or d-mm-yyyy with
// separators: periods, slahes, dashes
$p = '{.*?(\d\d?)[\\/\.\-]([\d]{2})[\\/\.\-]([\d]{4}).*}';
$date = preg_replace($p, '$3-$2-$1', $str);
return new \DateTime($date);
}
// verify that it works correctly for your values:
$arr = array(
"03/12/2011 (Sat)",
"3.12.2011 SAT",
"Date: 03/12/2011 ", /* <-- the extra trailing space is intentional */
"date:03/12/2011",
"date: 03/12/2011",
"03/12/2011"
);
foreach ($arr as $str) {
$date = date_grab($str);
echo $date->format('Y-m-d') . "\n";
}
you can use the below function, it will match all the variables you spcecified.
function extractDates($mydate)
{
$date = explode(" ", $mydate);
$output = $date[0];
if ($date[0] == "Date:" || $date[0] == "date:")
{
$output = $date[1];
}
return $output;
}
$date1 = "Date: 03/12/2011";
echo extractDates($date1);
The output will be as you expected: "03/12/2011".
You can also test all your strings:
$date1 = "03/12/2011 (Sat)";
$date2 = "3.12.2011 SAT";
$date3 = "Date: 03/12/2011 "; /* <-- the extra trailing space is intentional */
$date4 = "date:03/12/2011";
$date5 = "date: 03/12/2011";
$date6 = "03/12/2011";
$date7 = "13.12.2011 TUE";

Getting information from Google Calendar using php's domdocument

Below is the code. It seems to not be opening the google calendar at all. I believe it has something to do with the url I am using and possbily the special character. I get the following:
Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: I/O warning : failed to load external entity "https://www.google.com/calendar/feeds/tjd#dreilinginc.com/public/basic" on line 4
<?
$dom = new DOMDocument();
$feed = "https://www.google.com/calendar/feeds/tjd#dreilinginc.com/public/basic";
$html = $dom->loadHTMLFile($feed);
$dom->preserveWhiteSpace = true;
$entries = $dom->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( "l jS \o\f F Y - h:i A", strtotime( $startTime ) );
$places = $entry->getElementsByTagName( "where" );
$where = $places->item(0)->getAttributeNode("valueString")->value;
print $title . "\n";
print $when . " AST\n";
print $where . "\n";
print "\n";
}
}
?>
As far as I know, DOMDocument::loadHTMLFile() is capable of negotiating SSL, but if it is failing you might try file_get_contents() to read the file first into a string.
$dom = new DOMDocument();
$feed = "https://www.google.com/calendar/feeds/tjd#dreilinginc.com/public/basic";
$feed_string = file_get_contents($feed);
$html = $dom->loadHTMLFile($feed_string);
This is fully speculative though. Treat it as such.
EDIT Make sure that allow_url_fopen is enabled in your php.ini.

Categories