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>";
}
Related
I made a module that display how many days ago a article was published
it looks like this.
{source}
<?php
$jinput = JFactory::getDocument()->input;
$option = $jinput->get('option');
$view = $jinput->get('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& $jinput->get("content");
$article->load($article_id);
$date = new JDate($article->get("publish_up"));
$currentTime = new JDate('now');
$interval = $date->diff($currentTime);
if($interval->d == 0) {
echo 'dzisiaj' . "<br>";
}
else if( $interval->d == 1) {
echo 'wczoraj' . "<br>";
}
else if( $interval->d > 1) {
echo $interval->format('%a dni temu') . "<br>";
}
}
?>
{/source}
And it works on my local joomla but when use it on custom template it doesnt work. I'm using Joomla 3.4.8.
The issue is you're trying to access the input values using Document Factory that's wrong you have to use
$jinput = JFactory::getApplication()->input;
Document Factory is used for other purpose like adding , styles or Js to the pages etc. read more about input here.
Hope it make sense.
I want inteface this script with google calendar using Zend Framework.
This code works locally but doesn't works on altervista.
The error is
Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with
message 'Expected response code 200, got 401
<?php
require_once "Zend/Loader.php";
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
$user = "xxx#gmail.com";
$pass = "xxxxx";
$title= "Evento ";
$content="evento presso nome associaizone";
$where="luogo";
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
try {
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cl');
} catch (Zend_Gdata_App_CaptchaRequiredException $cre) {
echo 'URL of CAPTCHA image: ' . $cre->getCaptchaUrl() . "\n";
echo 'Token ID: ' . $cre->getCaptchaToken() . "\n";
} catch (Zend_Gdata_App_AuthException $ae) {
echo 'Problem authenticating: ' . $ae->exception() . "\n";
}
$service = new Zend_Gdata_Calendar($client);
//
// Create a new entry using the calendar service's magic factory method
$event= $service->newEventEntry();
// Populate the event with the desired information
// Note that each attribute is crated as an instance of a matching class
$event->title = $service->newTitle($title);
$event->content= $service->newContent($content);
$event->where = array($service->newWhere($where));
// Set the date using RFC 3339 format.
$startDate = "2014-03-18";
$startTime = "14:00";
$endDate = "2014-03-19";
$endTime = "16:00";
$tzOffset = "-08";
$when = $service->newWhen();
$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
$event->when = array($when);
// Upload the event to the calendar server
// A copy of the event as it is recorded on the server is returned
$newEvent = $service->insertEvent($event);
?>
Zend_Gdata and ZendGData are out of maintenance.
Instead the recomendation is migrate to the official Google SDK https://code.google.com/p/google-api-php-client/
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]));
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'];
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>";
}
?>