Zend Date MySQL Timezone offset problem - php

I've got a site which uses the Zend Framework. There's a form which the users fill in, including a Date field. Currently I'm using this to create a new Zend_Date object and then getting the date in ISO format to put into the MySQL database. However when the date is returned in ISO format it also has the timezone offset appended to the end (e.g. 2011-01-01T00:00:00-0500), which MySQL doesn't like. When I try to add it to the DB it gives me an invalid date error. I'm sure there must be a simple solution to return the date without the timezone offset, but I can't seem to find it.
Any suggestions?
Thanks.

if you toString your Zend_Date object and then use the mb_strcut() function to remove the timezone and then insert it into the db it should be fine ?
another way would be to alter the Zend function that returns the date in an ISO format to prevent it from appending the timezone to the end of the date.

Related

Mysql is not converting timestamp to the timezone of PHP query

I have a mysql database in this format
And I am trying to fetch the values through a php document and convert them into json on the timezone of the user (or maybe just GMT-6 would suffice) but the json outputs from the php document are as follows:
[{"timestamp":"2018-06-13 19:52:05","temperature":"79.83","humidity":"41.89","pressure":"99.35"},{"timestamp":"2018-06...
Still in UTC time, I have tried adding
date_default_timezone_set('America/Los_Angeles');
To the php document, but the time never changes, how would I solve this?
Use convert_tz() on the timestamps in your query to convert them from one timezone to another, e.g.:
... convert_tz(`timestamp`, 'Etc/UTC', 'America/Los_Angeles') ...
Make sure to follow the procedure "Populating the Time Zone Tables" as described in "5.1.12 MySQL Server Time Zone Support" to have the time zones available or to check which are available in your system.

ZF3 get date/time format based on locale

I'm looking for a way to get the date and time or date/time format based on the user locale I have.
Like:
de_DE will have d-m-Y (21-10-2017)
en_US will have Y/m/d (2017/10/21)
Is there a way in ZF3 to get this or do I need to use some PHP based solution ?
It's not the idea to get the anything formatted but the format itself.
What you are probably looking for is the zend-i18n view helpers.
If unset, it will use the default locale (return value of Locale::getDefault()).
So if you use it without setting the last parameter, you should be able to use the language defined in your previous topic!

time() to DateTime

I have an application that uses time() to record the time a topic was posted. I have done this for a long time and the only glitch i ever had with the method was that the time was always off by an hour (mainly a DST issue i never looked into).
I'm want to switch to the DateTime method, since I'm also switching to Twig, which uses that date format when setting a timezone.
But from what I can see, you can't use timestamps to parse the date. my question is, how do you input a date and parse it and what format are they looking for if it isn't time()?
By the sounds of it your server's default timezone is UTC. I suggest you try changing the default_timezone setting in php.ini to something that applies DST. This will mean that when you use date(), the resulting output will be correctly adjusted for DST.
you can't use timestamps to parse the date
There is a way, if your php version is >=5.3 :
$time = time();//or other time
$date = DateTime::createFromFormat('U',$time);
but as a matter of fact it is somewhat weird to use timestamp as DateTime does not rely on it. You can parse a date string directly with the datetime constructor if your date is english or "french" (i.e d/m/y). You can also create your custom format.
how do you input a date and parse it and what format are they looking for if it isn't time()
if it is a a standard format date :
$date = new DateTime($yourString);//would throw an exception if the format is wrong
if it is a custom format, like DD/MM/YYYY 14h32mins
$date = DateTime::createFromFormat('d/m/Y h\hi\m\i\n\s',$yourString);
if($date === false){//if format does not fit
exit('Your wrong !');
}
I have an application that uses time() to record the time a topic was posted.
Very bad practice. In MySQL you have DateTime format (and each relational data base system has his own equivalent) to store date times and there are fully compatible with the DateTime constructor in PHP so migrate to it (use FROM_TIMESTAMP to change into DateTime).

How to validate date in php?

In my project I am importing users from a csv file. In that some columns have date values
Eg:- date of birth, project start date, project dead line date etc. I know the date column headers but the user can enter date format in different ways. How can I validate the value get from csv is a valid date or not?
While not always accurate, you can try strtotime(). It's not a perfect solution but it's worth trying. Just read the notes because it can have varying behavior depending on what your date formats look like.
The php function strtotime can take many different date formats and returns a timestamp. If your can make thoughtful assertions on the valid timestamp values, validation can be done with those assertions.
But it could be prone to side effects,like day and month mismatch, so it would be a poor validation. I suggest that you enforce a common date pattern on the input file.
I use Zend_Date, as the constructor will validate the date for me, but of course if you don't expect a precise format you'll get in the kind problems that #gordon points out with 11/10/09 type of date. This will generate you a valid date, but for a default locale/format which can be of course wrong.

Best way to store date and time with MySQL PHP + AJAX

I'm making a web based project management application using MySQL and PHP that uses JS (Jquery) in the front end. The user has to input a date and optionally time as well.
However I'm not sure how I should go about inserting and storing the date and converting it back to human readable form in the application.
Thanks in advance,
RayQuang
Always use the standard Date/Time types for the respective situation.
In MySQL, use one of the appropriate Date and Time Type. Don't just blindly use one type. If you're storing a date, don't use a timestamp. If you're storing a timestamp, don't use a date. Use the proper type and be done.
In PHP, you can use an integer (parse from mysql's type with strtotime().
Talking with JS, I'd suggest using RFC 2822 date format, since it's standard. That way, you're communicating externally using a standard date/time format (which is non-ambiguous).
Store as timestamp.timestamp contain both date and time. and it will be best way to store date and time .

Categories