Showing timezone specific dates in views with Zend_Date - php

I'm storing all my dates in ISO-format, so all of them look like this:
2010-08-17T12:47:59+00:00
Now, when my application starts, I register the timezone the current user resides in. In my case, this would be "Europe/Berlin":
date_default_timezone_set("Europe/Berlin");
However, when Zend_Date parses ISO dates, it overrides the default timezone set earlier and now has the UTC timezone.
But when I output this date in my view scripts I want it to show the date in the correct timezone.
Are there better solutions than writing a custom view helper just for this? (If this was the correct solution, shouldn't there already be a "DateViewHelper"?)

Not sure how this works with Zend_Date but given the fact that you use PHP >= 5.2 you can use the built-in DateTime class (which offers less functionality but is extremely faster):
$date = new DateTime('2010-08-17T12:47:59+00:00');
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo $date->format(DateTime::W3C);
EDIT
Just checked Zend_Date and it actually works the same here...
$date = new Zend_Date('2010-08-17T12:47:59+00:00', Zend_Date::ISO_8601);
$date->setTimezone(date_default_timezone_get());
echo $date->getIso();

Related

Change the ISO 8601 date format using PHP

I am trying to get the current date/time using Data Type: ISODateTime and Format: YYYY-MM-DDTHH:MM:SS e.g. 2012-02-06T08:35:30. I searched how I would do this in PHP and found that I can use;
$formatedDate = date("c");
Although the output of this is almost correct is not quite what I need and I can't figure out how to alter it, the current output of this is;
2014-07-01T10:53:10+02:00
My problem is I need to remove the "+02:00" and also this time is an hour ahead of my local time, which is what I need. Therefore, in this example, I would require;
2014-07-01T09:53:10
Any help would be really appreciated. Thanks.
You could just format the date manually.
$formattedDate = date('Y-m-d\TH:i:s');
To get your local time, use date_default_timezone_set() to set the appropriate timezone (before declaring $formattedDate).
// Change 'America/New_York' to your timezone
date_default_timezone_set('America/New_York');
See demo
To find your timezone, see the List of Supported Timezones.

timestamp from both date & time inputs and compare php

I've been struggling to get an exact answer for this question. There are many that are close to what I'm wanting but seem to still be just off. The application of this is to ensure that a booking can't be made for a past date.
I have a form which has an input for time & another for date. Firstly, I wan't to take both of these inputs & convert them to a timestamp.
This code returns nothing
$time_date = sprintf("%s %s", $pDate, $pTime);
$objDate = DateTime::createFromFormat('H:ia d/m/Y', $time_date);
$stamp = $objDate->getTimestamp();
echo $stamp;
So I've have tried using something like this
$pDate = $_POST['pDate'];
$pTime = $_POST['pTime'];
$full_date = $pDate . ' ' . $pTime;
$timestamp = strtotime($full_date);
echo $timestamp;
But for some reason it is returning an incorrect timestamp. (i've been using an online converter) 02/06/2014 as date & 12:23am as time, is not 1401625380. This according to the converter is Sun, 01 Jun 2014 12:23:00 GMT.
Does someone have working code for returning a timestamp of both time & date inputs?
Secondly I want to compare this timestamp with a specified one & check to see if it is greater than. I've created a timestamp for my timezone with this
$date = new DateTime(null, new DateTimeZone('Pacific/Auckland'));
$cDate = $date->getTimestamp();
echo $cDate;
and will simply have an if statement which compares the two and echos the appropriate message.
I feel as though there are multiple question on here that are ALMOST what I'm wanting to achieve but I can't manage to get them working. Apologies for the near duplicate.
Note: I'm using ajax to post form data (if this could possibly interfere).
Your second code snipped is correct. Assuming it's in datetime format (Y-m-d H:i:s).
From php manual about strtotime():
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter.
Check your PHP default time zone with date_default_timezone_get() function.
To compare two dates, be sure they both are in same time zones.
For datetime inputs I personally use jQuery UI timepicker addon.
you receiving the time and date in string format - so i don't believe the ajax can interfere.
as for your question:
first of all - find out what is the locale timezone of your server. you can do it by this function: date_default_timezone_get.
if the answer doesn't suit you - you can use its "sister": date_default_timezone_set, and change it to whatever value you need (like 'Pacific/Auckland' - see the documentation there). it is also recommended to return it to the original value after you finish your stuff.
i believe fixing your locale timezone will solve your issue.

Manage PHP time function

I am using godaddy hosting service and I can manage local time, I have to use the server default time that is America/Phoenix.
Even if i am using date_default_timezone_set("Asia/Kolkata");
function in my config file then also there is no difference in time and godaddy people are not ready to help me with, I am tired of calling this guys but no response, I hate them all.
Is there any means I can get my local time using any function or any external API?
I am using this code
//set time zone india
date_default_timezone_set("Asia/Kolkata");
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
$date = date('m/d/Y h:i:s a', time());
echo $date."<br>";
and the output that I am getting is
The current server timezone is: Asia/Kolkata05/14/2014 12:06:26 am
Even if it is 4:38 pm here...
date_default_timezone_set should work for PHP functions like date, however I'm going to hazard a guess that you're having problems with date/time elements in a database such as MySQL.
I know I've had similar problems before, when trying to get everything on UTC instead of Europe/London...
When you've established the connection to your database, be sure to run this query:
SET time_zone = 'Asia/Kolkata';
This, in addition to date_default_timezone_set, should solve your problems. However, if you're using DATETIME columns, then those will not be fixed. TIMESTAMP columns will be automagically fixed to the new timezone because they are saved as UTC internally and converted upon retrieval.
You shouldn't touch date_default_timezone_set. The proper way to do that would be to use DateTimeZone object. Something like this:
$now = new \DateTime('now', new \DateTimeZone('Asia/Kolkata'));
echo $now->format(\DateTime::ATOM);
So, the idea is that you create an object in a timezone of server and then convert it to your timezone

Timezone is correct but Time is not - PHP MySQL

I'm making a little twitter clone just for learning and I came across a problem where, before and after allowing users to select the timezone they are in, the time displaying when they would tweet a certain thing was wrong.
Here are snippets of my code:
/* current date/time whenever they send a tweet */
$time = date('Y-m-d H:i:s');
/* Insert into db as `time` */
/* Time retrieved as $value['time'] & users timezone as $_SESSION['timezone'] */
/* Convert to users timezone */
$users_timezone = new DateTimeZone($_SESSION['timezone']);
$date = new DateTime($value['time']);
$date->setTimeZone($users_timezone);
$new_date = $date->format('M j, o g:i a e');
echo $new_date;
It is currently 11:32am here in the LA area, yet after conversion it shows 6:26pm
My default is in Berlin, which it is currently 8:33pm but before conversion it shows 1am
Can anyone give me any insight into this? First time doing this.
Check System Time
Please check your current system time of your server by running date via ssh.
I believe php gets the date from the system therefore if your system time is incorrect then your php time would also be incorrect.
Check $value['time']
You are using the construct method within the DataTime class. Here is the documentation for that method.
http://www.php.net/manual/en/datetime.construct.php
Make sure $value['time'] is in an acceptable format. On way to do this easily is to use the strtotime function. This will make a unix timestamp from $value['time'] and that will probably satisfy the construct method of the DateTime class.

PHP Zend date format

I want to input a timestamp in below format to the database.
yyyy-mm-dd hh:mm:ss
How can I get in above format?
When I use
$date = new Zend_Date();
it returns month dd, yyyy hh:mm:ss PM
I also use a JavaScript calender to insert a selected date and it returns in dd-mm-yyyy format
Now, I want to convert these both format into yyyy-mm-dd hh:mm:ss so can be inserted in database. Because date format not matching the database field format the date is not inserted and only filled with *00-00-00 00:00:00*
Thanks for answer
Not sure if this will help you, but try using:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
Technically, #stefgosselin gave the correct answer for Zend_Date, but Zend_Date is completely overkill for just getting the current time in a common format. Zend_Date is incredibly slow and cumbersome to use compared to PHP's native date related extensions. If you don't need translation or localisation in your Zend_Date output (and you apparently dont), stay away from it.
Use PHP's native date function for that, e.g.
echo date('Y-m-d H:i:s');
or DateTime procedural API
echo date_format(date_create(), 'Y-m-d H:i:s');
or DateTime Object API
$dateTime = new DateTime;
echo $dateTime->format('Y-m-d H:i:s');
Don't do the common mistake of using each and every component Zend Frameworks offers just because it offers it. There is absolutely no need to do that and in fact, if you can use a native PHP extension to achieve the same result with less or comparable effort, you are better off with the native solution.
Also, if you are going to save a date in your database, did you use any of the DateTime related columns in your database? Assuming you are using MySql, you could use a Timestamp column or an ISO8601 Date column.
This is how i did it:
abstract class App_Model_ModelAbstract extends Zend_Db_Table_Abstract
{
const DATE_FORMAT = 'yyyy-MM-dd';
public static function formatDate($date, $format = App_Model_ModelAbstract::DATE_FORMAT)
{
if (!$date instanceof Zend_Date && Zend_Date::isDate($date)) {
$date = new Zend_Date($date);
}
if ($date instanceof Zend_Date) {
return $date->get($format);
}
return $date;
}
}
this way you don't need to be concerned with whether or not its actually an instance of zend date, you can pass in a string or anything else that is a date.
a simple way to use Zend Date is to make specific function in its business objects that allows to parameter this function the date format. You can find a good example to this address http://www.pylejeune.fr/framework/utiliser-les-date-avec-zend_date/
this is i did it :
Zend_Date::now->toString('dd-MM-yyyy HH:mm:ss')
output from this format is "24-03-2012 13:02:01"
and you can modified your date format
I've always use $date->__toString('YYYY-MM-dd HH-mm-ss'); method in the past but today didn't work. I was getting the default output of 'Nov 1, 2013 12:19:23 PM'
So today I used $date->get('YYYY-MM-dd HH-mm-ss'); as mentioned above. Seems to have solved my problem.
You can find more information on this on output formats here: http://framework.zend.com/manual/1.12/en/zend.date.constants.html

Categories