PHP Zend date format
Asked Answered
M

6

14

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

Mccarter answered 1/6, 2011 at 13:18 Comment(4)
do you use Zend_Date for anything else? Or do you just want to format a timestamp with it?Conchita
Have you referred to the manual before asking? framework.zend.com/manual/en/zend.date.constants.htmlJeromyjerreed
@Coding-Freak yes I referred manual but, I found this 2009-02-13T12:53:27+01:00. I don't want "T" and "+" sign in between.Mccarter
@Gordon: I used zend_date() to get the current date.Mccarter
I
26

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') 
Intoxication answered 1/6, 2011 at 13:29 Comment(6)
Thanks. It works $date = new Zend_Date(); $createdDate= $date->get('YYYY-MM-dd HH:mm:ss'); print($createdDate);Mccarter
@Mccarter If an answer solves your problem, it's considered polite to accept the given answer.Hippolytus
I have got a date data from a textbox in form in an arry like $formData['startdate'] which is in dd-mm-yy format. Now, how to convert in that format by using above code or any other technique?Mccarter
In the same style, use $date = new Zend_Date($stringDate,'dd-MM-YY') or later $date->set($stringDate,'dd-MM-YY')Type
You might get weird results for YYYY for dates after the Xmas, better use yyyy-MM-dd ...Wardieu
This just bit me. Use yyyy, not YYYY unless you specifically understand a reason not to. You will get arbitrarily wrong dates around the last few days of the year if you don't!Weinrich
C
14

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.

Conchita answered 1/6, 2011 at 13:36 Comment(7)
I second that! ;) Gordon always knows best.Intoxication
@Gorden: I got it. But I want to use array value of$formData['startdate'] in first parameter of date_format(date_create(), 'Y-m-d H:i:s');. i.e. in place of date_create().Mccarter
@Mccarter sorry but I won't tell you how to do that. Working with dates is basic knowledge for a PHP developer. I really mean no offense, but if you dont know how to do that, you shouldn't be working with Zend Framework but get to grips with PHP basics first. Please follow the links to the documentation and try to find out yourself. It's easy and it is absolutely essential that you learn how to do it.Conchita
Only exception to using Zend_Date is when you need advanced date manipulations like comparing and stuff. For that is Zend_Date much simplier than pure php functions. But for db saving/displaying date it's just pure overkill ;)Pharisaism
And ultimate answer for date manipulation is date('Y-m-d', strtotime($nearlyAnyNormalDateFormat));Pharisaism
;o) Ah Gordon! ;) Happy to have you chime in. Indeed, I totally get your point. Wich brings me to a question, should one avoid using the framework for small tasks when possible? Would not that bring us back to spaghetti code interwined with whatever-framework-lib? I use drupal and actually like their implementation of date handling better than PHPs but in other cases - the mailing and notification apis for instance I totally disregard drupal`s klonky notification approach. Having never met Sire Gordon personally, I can attest to wondering if this guy is human at all! :) THanks friend.Intoxication
@stefgosselin IMO, one should avoid a framework when it doesn't offer a significant benefit over a native API. Benefit being readability or ease of use or performance or speed of development, etc. As for spaghetti code: no. Just because you use a native API doesnt mean it will be Spaghetti code. Spaghetti code is always written by people. It's perfectly possible to write spaghetti code with a framework, too.Conchita
P
4

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.

Procuration answered 1/6, 2011 at 20:8 Comment(0)
D
3

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/

Daphie answered 23/10, 2012 at 18:56 Comment(1)
"allows to parameter this function the date format" is rather unclear. Can you please rephrase? Also, welcome to StackOverflow!Esquimau
C
1

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

Combinative answered 1/4, 2013 at 2:33 Comment(0)
L
0

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

Lanny answered 1/11, 2013 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.