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

Gordon
  • 312,688
  • 75
  • 539
  • 559
maniclorn
  • 1,081
  • 3
  • 12
  • 23

6 Answers6

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') 
Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
  • Thanks. It works `$date = new Zend_Date(); $createdDate= $date->get('YYYY-MM-dd HH:mm:ss'); print($createdDate);` – maniclorn Jun 01 '11 at 13:34
  • @maniclorn If an answer solves your problem, it's considered polite to accept the given answer. – Berry Langerak Jun 01 '11 at 13:36
  • 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? – maniclorn Jun 01 '11 at 13:43
  • In the same style, use `$date = new Zend_Date($stringDate,'dd-MM-YY')` or later `$date->set($stringDate,'dd-MM-YY')` – Arek Jablonski Jun 01 '11 at 16:11
  • 1
    You might get weird results for YYYY for dates after the Xmas, better use yyyy-MM-dd ... – rukavina Feb 10 '14 at 09:25
  • 1
    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! – David McHealy Oct 14 '16 at 18:32
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.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 3
    I second that! ;) Gordon __always__ knows best. – Stephane Gosselin Jun 01 '11 at 13:42
  • @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(). – maniclorn Jun 01 '11 at 13:57
  • @maniclorn 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. – Gordon Jun 01 '11 at 14:03
  • 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 ;) – Tomáš Fejfar Jun 01 '11 at 16:54
  • 1
    And ultimate answer for date manipulation is `date('Y-m-d', strtotime($nearlyAnyNormalDateFormat));` – Tomáš Fejfar Jun 01 '11 at 16:55
  • ;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 PHP`s but in other cases - the mailing and notification api`s 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. – Stephane Gosselin Feb 11 '13 at 17:23
  • 1
    @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. – Gordon Feb 11 '13 at 17:43
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.

Fatmuemoo
  • 2,187
  • 3
  • 17
  • 34
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/

lothar
  • 31
  • 1
  • "allows to parameter this function the date format" is rather unclear. Can you please rephrase? Also, welcome to StackOverflow! – Anuj Gupta Oct 23 '12 at 19:22
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

akubabas
  • 473
  • 5
  • 13
  • 28
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

Lionel Morrison
  • 566
  • 4
  • 15