Need help formatting Unix Date from XML output. - facebook

I have an events feed from facebook that outputs data in XML format. The dates/ times are in epoch (unix) format, I think. Like so:
<start_time>1319506200</start_time><end_time>1319511600</end_time>
This is dynamic information (events created by a facebook page).
I am using php file_get_contents to place the xml output in my html.
How in the world can I convert the unix dates to a user friendly format? I am at a total loss.

Simply use string date ( string $format [, int $timestamp = time() ] )
Extract the timestamp value from the xml string and pass in as argument #2 to date, as argument #1 you should supply your preferred format string, for example 'Y-m-d T:i:s'
Extracting he timestamp could probably look something like this:
Given that you have the event feed output data stored in a string, in this case $xmlstr
$facebooksomething = new SimpleXMLElement($xmlstr);
date('Y-m-d T:i:s', $facebooksomething->starttime);

Finally got this figured out! :) Woohoo! (changed from the xml feed to json feed by the way) This is what ended up working:
$start_date = date('F j, Y, g:i a', strtotime($json_output2->start_time));
$end_date = date('g:i a', strtotime($json_output2->end_time));
Here is my full code:
<?php
$jsonurl = "https://graph.facebook.com/PAGEID/events?access_token=MYYOKEN";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->data as $data)
{
$jsonurl2 = "https://graph.facebook.com/$data->id/";
$json2 = file_get_contents($jsonurl2,0,null,null);
$json_output2 = json_decode($json2);
$start_date = date('F j, Y, g:i a', strtotime($json_output2->start_time));
$end_date = date('g:i a', strtotime($json_output2->end_time));
echo "{$json_output2->name}\n";
echo "<br>";
echo "{$start_date}\n";
echo " - ";
echo "{$end_date}\n";
echo "<br>";
echo "{$json_output2->description}\n";
echo "<br>Where: ";
echo "{$json_output2->location}\n";
echo "<br><br>";
}
?>

Related

Extracting data with preg_match

I'm trying to extract Facebook members name with preg_match from gaph.facebook.com, by userID. The script doesn't seem to be working. can anyone help?
<?php
$content = file_get_contents('https://graph.facebook.com/myid?fields=id,name,first_name,last_name,picture');
preg_match("'\"name\": \"(.*?)\",'si", $content, $match);
$name = $match[1];
?>
I don't understand why would you use PHP preg_match() with REGEX instead of using json_decode() since you're already dealing with a JSON type response
<?php
$content = file_get_contents('https://graph.facebook.com/{ID}?fields=id,name,first_name,last_name,picture');
$jsonData = json_decode($content);
echo '</br> id: ' .$jsonData->id,
'</br> name: ' .$jsonData->name,
'</br> first_name: ' .$jsonData->first_name,
'</br> last_name: ' .$jsonData->last_name,
'</br> picutre url: '.$jsonData->picture->data->url;
?>
Example

Zend_Config_Xml

So i will list my problems here, please help
First of all, i use Zend_Http_Client to call to an url:
$client = new Zend_Http_Client('http://mydomain.com/Statistics.aspx?activity=top');
$response = $client->request('POST');
$response = $response->getRawBody();
Then i get an Xml document structure for $response when i print it out:
[?xml version='1.0' encoding='UTF-8'?]
[root]
[member]
[username>gh_MXH[/username]
[money]129300[/money]
[/member]
[member]
[username]sonhothiet_MXH[/username]
[money]107400[/money]
[/member]
[/root]
After that, i use Zend_Config_Xml:
$xmlReader = new Zend_Config_Xml($response);
$xml = $xmlReader->toArray();
But all i get is just only the first element of the array when i print out $xml:
Array
(
[member] => Array
(
[username] => gh_MXH
[money] => 129300
)
)
Please show me how to get all the elements so that i can do the looping like:
foreach($xml as $key => $value){
echo $value['username'] . 'has' . $value['money'];
}
And one more question, when i wrote:
$xml = new Zend_Config_Xml($response, 'root');
It appears an error saying: Section 'root' cannot be found in. Really need your help. Thank you and sorry for bad English.
Don't use Zend_Config_Xml to parse standard XML docments, it is for config files. PHP can easily do this natively:
$xml = simplexml_load_string($response);
foreach($xml->member as $member){
echo $member->username . 'has' . $member->money;
}

Which is correct method to compare two dates in PHP?

I've following code to compare two dates in PHP,which one while be appropriate method
<?php
$var = date('d-m-Y',strtotime('29-05-2012'));
$var1 = date('d-m-Y',strtotime('27-06-2012'));
echo $var; //29-05-2012
echo $var1; //27-06-2012
if($var1 >= $var) //method 1
{
echo 'var1 is future date';
}
if(strtotime($var1) >= strtotime($var)) //method 2
{
echo 'var1 is future date(second if)';
}
?>
In above two methods,method-1 is not working,is it not a correct way to compare two dates in PHP ?
No, the first method is incorrect because $var1 and $var are strings, so you can't compare them like that.
However, strtotime() creates unix timestamps (integers), so you can and should compare them like that.
Just leave the date as string, and convert with strtotime in if ().
$a = '29-5-2012';
$b = '27-6-2012';
if (strtotime($a) >= strtotime($b)) {
echo "$a is future date.";
} else {
echo "$b is future date.";
}
// 27-6-2012 is future date.
Depending on you php version >= 5.3 you can try date_diff()
$time1 = strtotime('29-05-2012'); # <--- past
$time2 = strtotime('27-06-2012'); # <--- future
echo max($time1,$time2);
echo "<br />";
echo min($time1,$time2);
why not:
if (mktime(0,0,0,12,31,2012) > mktime(0,0,0,6,25,2011)) {
echo "12/31/2012 is after 6/25/2011";
}
$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));

date functionality

I want to print 7 days span for a particular date.. I have tried reading php manual and tried several things..nothing is working out.
<?php
function add_date($givendate,$day=0,$mth=0,$yr=0) {
$cd = strtotime($givendate);
$newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
date('i',$cd),
date('s',$cd),
date('m',$cd)+$mth,
date('d',$cd)+$day,
date('Y',$cd)+$yr));
return $newdate;
}
?>
but this is not giving me any date.except today's date.
you can use DateTime class:
For example:
$today = new DateTime("now");
$yesterday = $today->modify('-1 day');
$yesterday = get_object_vars($yesterday);
echo $yesterday['date']."<br>";
$twoDaysAgo = $today->modify('-1 day');
$twoDaysAgo = get_object_vars($twoDaysAgo);
echo $twoDaysAgo['date'];

Zend Google Calendar API - How to Change DateTime Format?

I am using the Zend Framework Gdata for the Google Calendar API and the datetime outputs in RFC3339 (which looks like this: 2012-01-19T22:00:00.000-08:00). What php code do I need to add in order to convert that to UTC so that it looks like this: January 19, 2012 8pm-11pm ? I have found php code that converts RFC dates to string, but I don't know enough about php to be able to alter the code to work with the Zend Gdata formulas... as you will see in my code below, the "date" is referred to as "when"... so any code will have to connect those two somehow... any help is appreciated!
<?php
$path = '/home/ZendGdata/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
// User whose calendars you want to access
$user = 'my#email.com';
$pass = 'mypassword';
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Calendar($client);
$query = $service->newEventQuery();
// Set different query parameters
$query->setUser('mycalendarID');
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
// Start date from where to get the events
$query->setStartMin('2012-01-01');
// End date
$query->setStartMax('2050-03-15');
// Get the event list
try {
$eventFeed = $service->getCalendarEventFeed($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
echo "<ul>";
foreach ($eventFeed as $event) {
echo "<tr>";
foreach ($event->when as $when) {
echo "<td>" . $when->startTime . "</td>";
}
echo "<td>" . $event->content . " </td>";
$where=$event->Where;
foreach($where as $eventplace)
{
echo "<td>" . $eventplace . " </td>";
}
}
echo "</tr>";
echo "</ul>";
?>
Thank you for this information #vascowhite.
Two issues:
The output turned out like this:
string(23) "20 January 2012: 06:00"
I am pulling this info from my google calendar, and it is outputting into my html page...I am not not creating new events through this php...so this code simply converted the date that you wrote, it didn't convert my google calendar event date which is pulled from this code:
foreach ($event->when as $when) {
echo "<td>" . $when->startTime . "</td>";
}
Do you know how to do that?
Thank you,
Susan
You can use PHP's DateTime class to do this quite easily.
First create a DateTime object from your time string:-
$timestr = '2012-01-19T22:00:00.000-08:00';
$date = new DateTime($timestr);
That object is in the correct time zone because of the '-08:00' part of the string.
Now decide which time zone you want to convert to and create a DateTimeZone object (I have chosen UTC as you specifically mention it):-
$tz = new DateTimeZone('UTC');
$date->setTimezone($tz);
Your DateTime object has now been converted to the UTC time zone.
You can get it into your desired format by using the DateTime::format() method:-
var_dump($date->format('d F Y: H:i'));
Output:
20 January 2012: 06:00
To fit into your code:-
foreach ($event->when as $when) {
$date = new DateTime($when->startTime);
echo "<td>" . $date->format('d F Y: H:i') . "</td>";
}