I have two problems in my little script. I am trying to create an application that use Zend Framework 1.12.1, Doctrine, Zend_locale, Zend_Currency and Zend_Date.
Dates:
The database needs a date in this format Y-m-d H:i:s but each user set their locale preferences in the custom control panel. So when I get a date from the database I have to convert it in the locale choosen.
This is my code:
<?php
$dboutdata = "4/3/13 12:00 AM"; // 4 March 2013 12:00 AM
$date = new Zend_Date($dboutdata, null, "en_US");
echo $date . "<br/>";
$date = new Zend_Date($dboutdata, null, "it_IT");
echo $date . "<br/>";
?>
this is the result:
Apr 3, 13 12:00:00 AM
04/mar/13 12.00.00
As you can see the result is wrong. Zend_Date confuses the month with the day. How have I to solve this problem?
updates:
/**
* Convert a date from yyyy/mm/dd formatted by the locale setting
*
* #param date $dbindata
* #return date formatted by the locale setting
*/
static public function formatDateOut($dbindata) {
if (empty ( $dbindata ))
return false;
$locale = Zend_Registry::get('Zend_Locale');
$date = new Zend_Date($dbindata, "yyyy-MM-dd HH:mm:ss", $locale);
return $date->toString(Zend_Date::DATETIME_SHORT, $locale);
}
/**
* Convert a date from Zend Locale selected to yyyy/mm/dd H:i:s
*
* #param string $dboutdata
* #return string Y-m-d H:i:s
*/
static public function formatDateIn($dboutdata) {
if (empty ( $dboutdata ))
return null;
$InDb = null;
$locale = Zend_Registry::get('Zend_Locale');
$date = new Zend_Date($dboutdata);
return $date->toString('yyyy-MM-dd HH:mm:ss');
}
Furthermore, If I need to save this date how can I normalize it in order to save it in the database?
Best Regards
By passing the locale into the Zend_Date constructor, you're telling it what format the date is in. The US date format has the month first, where as the European date format has the day, so that's why you get different results.
You probably just want to set the locale when outputting the date, e.g.:
$dboutdata = "4/3/13 12:00 AM";
$date = new Zend_Date($dboutdata, null, "en_US");
echo $date->toString(null, 'en_US') . '<br>'; // outputs Apr 3, 13 12:00:00 AM
echo $date->toString(null, 'it_IT') . '<br>'; // outputs 03/apr/13 00.00.00
Related
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 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>";
}
Hello i am using Zend_currency
class Currency extends Zend_View_Helper_Abstract
{
public function currency($number, $locale = 'it_IT') {
$currency = new Zend_Currency($locale);
$number = $number + 0.00;//convert to float
return $currency->toCurrency((float) $number);
}
}
in a some view .phtml file
echo $this->currency($gimme_my_money);
and this is what i get
€ 19.373,25
-€ 116,07
how can i get it to print negative numbers like
€ -116,07
Just overwrite the format option like this:
$cur = new Zend_Currency(array('format' => '¤ #,##0.00;¤ -#,##0.00'));
The trick is in the second part of the string (after the comma), I've checked it for Italian locale and the format string provided there is ¤ #,##0.00.
This is tested with ZF 1.11.7
I don't think this formatting option is built into Zend_Currency.
What you can do, is move the currency symbol to the right side:
$this->view->total = new Zend_Currency(array('value' => $total, 'position' => Zend_Currency::RIGHT));
And then your currencies will be displayed with the currency symbol on the right:
-19.373,25 €
If you want a custom formatting, with the negative sign after the symbol, (€ -116,07), you will have to write your own currency formatter or built on top of Zend_Currency
try this:
class My_View_Helper_Currency extends Zend_View_Helper_Abstract
{
/**
* Format a numeric currency value and return it as a string
*
* #param int|float $value any value that return true with is_numeric
* #param array $options additional options to pass to the currency
* constructor
* #param string $locale locale value
*
* #throws InvalidParameterException if the $value parameter is not numeric
* #return string the formatted value
*/
public function currency($value, $options = array(), $locale = null)
{
if (!is_numeric($value)) {
throw new InvalidArgumentException(
'Numeric argument expected ' . gettype($value) . ' given'
);
}
$options = array_merge($options, array('value' => $value));
$currency = new Zend_Currency($options, $locale);
return $currency->toString();
}
}
Is there a way of converting a time, say 17:00 to 5:00pm using Zend Locale?
I've tried the method in the docs as it is (which has a typo), but it doesn't work. It gives the error 'Unable to parse date '13:44:42' using 'dd.MM.yyyy' (M <> y)'
$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::getTime('13:44:42',
array('date_format' =>
Zend_Locale_Format::STANDARD,
'locale' => $locale))) {
print "time";
} else {
print "not a time";
}
I then tried a 2 step method, getting the time format of the current locale first, and then using that in the getTime function.
$locale = new Zend_Locale('en_US');
$tf = Zend_Locale_Format::getTimeFormat($locale);
$test = Zend_Locale_Format::getTime('17:00', array('date_format' => $tf, 'locale' => $locale));
This returns a result but just gives me back what I had
array('date_format'=>'h:mm:ss a', 'locale'=>'en_US', 'hour'=>'17', 'minute'=>'00')
Is there something that will convert the time to the actual locale I'm trying to parse it to?
You need to be using Zend_Date with the locale to get the date in the format you want.
$date = new Zend_Date(); // Default ISO format type & en_US
// Set the time and pass the format I am using to set the time
$date->setTime('17:00:00', 'HH:mm:ss');
echo $date; // Jul 15, 2011 5:00:00 PM
EDIT
More on how you can use Zend_Locale with Zend_Date
$locale = new Zend_Locale('de_AT');
echo $date->toString(Zend_Locale_Format::getTimeFormat($locale)) ; // 17:00:00
$locale = new Zend_Locale('en_US');
echo $date->toString(Zend_Locale_Format::getTimeFormat($locale)) ; // 5:00:00 PM
I'd like to use Zend_Date to print out the previous 2 months and year as a string e.g.:
July 2009
June 2009
I need it to be locale aware so that if the code runs with the locale set to, say, German, the month names will print in German.
$date = new Zend_Date();
$date->subMonth(1);
echo $date->get(Zend_date::MONTH_NAME).' '.$date->get(Zend_Date::YEAR);
$date->subMonth(1);
echo $date->get(Zend_date::MONTH_NAME).' '.$date->get(Zend_Date::YEAR);
Is this all I need to do?
thanks
You can just use the optional locale parameter in the get method:
$date = new Zend_Date();
echo $date->get(Zend_Date::MONTH_NAME,'de_DE');
echo $date->get(Zend_Date::MONTH_NAME,'en_UK');
Specify the locale when creating the Zend_Date object. Like this:
$date = new Zend_Date(new Zend_Locale('de_AT'));
$date->subMonth(1);
echo $date->get(Zend_date::MONTH_NAME).' '.$date->get(Zend_Date::YEAR);
$date->subMonth(1);
echo $date->get(Zend_date::MONTH_NAME).' '.$date->get(Zend_Date::YEAR);