FFSS
FFSS

Reputation: 133

Date format with Zend and Doctrine

I am working with Zend and Doctrine and collecting a date in the format of dd/mm/yyyy using a jquery calendar. I need to format it to enter into MySql as yyyy/mm/dd.

I have recently started working with Zend and Doctrine so not sure where and how this formatting should be done. Can please someone help me?

Upvotes: 2

Views: 1057

Answers (3)

JellyBelly
JellyBelly

Reputation: 2431

I counsel you to jquery-datepiker use of this possibility with the Italian localization, in order to have the calendar in Italian, but to set the format datepiker yyyy-mm-dd so I have no problems in saving it in the database in the data was taken for any changes.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/i18n/jquery.ui.datepicker-it.js"></script>

//DatePicker
$('.inputDate').datepicker({
    dateFormat: 'yy-mm-dd'
});

OR

Now it occurs to me that if you insist on your choice you may use the behaviors of Doctrine: http://www.doctrine-project.org/blog/using-behaviors-to-share-relationship-properties

look the example setPassword.

You could implement something like this:

class NameModel extends BaseNameModel
{
    public function setDate_name($date_name)
    {
        list($d, $m, $y) = explode("-", $date_name);
        $newDateName = $y."-".$m."-".$d;
        $this->_set('date_name', $newDateName);
    }
}

Upvotes: 1

dinopmi
dinopmi

Reputation: 2673

I don't think you need Zend Framework or Doctrine to do this. You could just write something like the following, using a regexp:

$output = preg_replace('#(\d+)/(\d+)/(\d+)#', '\3/\2/\1', $jQueryInput);

If you want a less "immediate" solution (i.e., more extensible), you can also use a date/strtotime combination.

Hope that helps,

Upvotes: 0

jerrymouse
jerrymouse

Reputation: 17812

I am giving you the php function which will take date as dd/mm/yyyy and return yyyy/mm/dd date.

function dateconvert($dateOriginal)
{
    $dateOriginal = preg_replace("/[^0-9\/]/","",$dateOriginal); //sanitizing
    $temp = explode("/",$dateOriginal);
    $dateNew = $temp[2]."-".$temp[1]."-".$temp[0];
    $dateNew = date("Y-m-d", strtotime($dateNew));
    return $dateNew;
}

Upvotes: 0

Related Questions