Customer Portal

Get Involved. Join the Conversation.

Topic

    Pramod Vasudeva Murthy
    Setting a Date field in CP - Very tiring Process !!!:(Answered
    Topic posted May 28, 2016 by Pramod Vasudeva MurthyBlack Diamond: 60,000+ Points 
    729 Views, 23 Comments
    Title:
    Setting a Date field in CP - Very tiring Process !!!:(
    Content:

    Hi All,

    This has been playing games with me from past 3 days; still not able to crack.

    I have a Custom object's date field called TestDate.

    I am getting date in this format from the form - 12/02/2015(dd/mm/yyyy). I have tried following methods and they all seem to either behave/work properly for one set of dates and work unlike.

    Sometimes this field is set to 01/01/1970. I have fought enough with this and finally gave upindecision.

    Please suggest an alternative.

    Thanks in advance,

    - VIP

    Version:
    Feb 2016
    Code Snippet:

    Best Comment

    Anuj Behl

    Hi Pramod,

    Thanks for trying it out. Now that you are able to get a working date format accepted by your custom field, we can work to fix the variation. You'll need to send the timezone along with the date while passing it to strtotime() method. Please check Rajan's jsfiddle above, you can use that to convert your date into ISO formatted date with timezone and pass that to strtotime. This way there won't be any variations.

    Comment

    • Anuj Behl

      Hi Pramod,

      Can you explain 'breaks for other dates'? What output do you get?

      Also, always try to use the ISO 8601 date format (example, "2007-04-05T14:30Z" or "2007-04-05T14:30") when working with date or date/time fields. This is a universally accepted format and almost all applications work with it.

      -Anuj

    • Pramod Vasudeva Murthy

      Hi Anuj,

      Thanks for your reply,

      By 'breaks for other dates' I mean a method of setting date works for one date for eg; 02/02/2016(mm/dd/yy) but doesn't set the same for other dates for rg: 12/02/2016 sets to 01/01/1970.

      Not sure why this behaviour is: Can you please share a sample code of setting a date field in CP? I get the input from the form in this manner(12/02/2016 {dd/mm/yyyy}).

      - VIP

    • Anuj Behl

      I would suggest you to try a hard-coded date value in ISO 8601 format and let us know the results. Try it with different inputs but in same format.

    • Barrilito van Dijk

      Hi Pramod,

      Good tip from Anuj to go back to the simple form first and try it hardcoded. I all ways try to fill it hardcoded, if that works you can fill it dynamically.

      I guess perhaps you are missing the setting of the default timezone, try it like this:

      date_default_timezone_set('UTC');
      $date = date('Y-m-dTH:i:s',mktime(0, 0, 0, 7, 1, 2000)).'Z';

      Regards.

    • Narendra Muttineni

      Hi Pramod,

      As per Barrilito, Please try to set the Timezone for method 3. because with the method three, there is one day difference with the date. so try to set the date with the Time zone.

      Regards,

      Narendra

    • Pramod Vasudeva Murthy

      date_default_timezone_set('UTC');
      $date = date('Y-m-dTH:i:s',mktime(0, 0, 0, 7, 1, 2000)).'Z';

      Throws a Numberformat exception stating a numeric value expected. And I have type-casted to a number like this:
       

      date_default_timezone_set('UTC');
      $date = date('Y-m-dTH:i:s',mktime(0, 0, 0, 7, 1, 2000)).'Z';
      $web_form_1->TestDate = intval($date);

      This sets date to '01/01/1970'.

      - VIP

    • Anuj Behl

      Hi Pramod,

      Did you try hardcoding the dates to narrow down to a format? How about 

      date_default_timezone_set('UTC');
      $date = date('Y-m-dTH:i:s',mktime(0, 0, 0, 7, 1, 2000)).'Z';
      $web_form_1->TestDate = $date;

      or

      $web_form_1->TestDate = "2007-04-05T14:30Z";

    • Barrilito van Dijk

      Hi Pramod,

      Excuses, I gave you an example of something that was being used for something else.

      You were sort of on the right direction in your first setup.

      As the documentation state here, everything should be stored as time, meaning as the time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). In your last examples were it did not work, it got a string and perhaps evaluated it to zero so you get the 1970 date.

      I guess if you do this it should work:

      $datetime="2012-01-12 00:00:00";
      $time=strtotime($datetime);
      $web_form_1->TestDate = $time;
      

      ps. in your example 2 I guess you are missing the setting of the local which gives the difference.

      Hope this will help.

      Regards.

    • Rajan Davis

      Just an idea, but if you are building web form, it might be easier to parse the date using Javascript than parse it on the backend. I have been building a fairly large Angular front end with ConnectPHP on the back end and parsing the date time with javascript has been fairly straightforward.

      Let's say you have a date field compose of 3 select elements representing the year, month, and day. Here is some pseudo code that you can try; first, collect the values that you will need:

      var year = document.querySelectorAll('<some unique identifier for the year>')[0].value;

      var month = document.querySelectorAll('<some unique identifier for the month>')[0].value - 1;

      //months are done on a zero based index in javascript, they start at 0 and end at 11

      var date = document.querySelectorAll('<some unique identifier for the day>')[0].value;

      Now you want it to be a date object:

      var desired_date = new Date(year,month,date);

      Convert the date to the needed format:

      var desired_date_final = desired_date.toISOString();

      Tested in the console:

      results in the console

      Let me know if that helps.

    • Pramod Vasudeva Murthy

      Hi All,

      Thanks for your comments,

      I have done the following:

      1. Hard coded these values and tried: but provides a number format exception

      date_default_timezone_set('UTC');
      $date = date('Y-m-dTH:i:s',mktime(0, 0, 0, 7, 1, 2000)).'Z';
      $web_form_1->TestDate = $date;
      or

      $web_form_1->TestDate = "2007-04-05T14:30Z";

      2. This works:

      $datetime="2012-01-12 00:00:00";
      $time=strtotime($datetime);
      $web_form_1->TestDate = $time;

      But problem here is I have date coming from form in this format: 28/02/2014

      Is conversion necessary or is there a way to set this as is using minimal conversions?

      3. One suggested by Rajan will not work for me because I am not using a OOTB date widget, I am using JQuery's date-picker.

      - VIP

    • Anuj Behl

      Hi Pramod,

      Did you try with your own examples other than what we suggested? What did you get when you tried strtotime("28/02/2014"); ? Please don't look for a copy-paste solution.

      strtotime() should accept date in any valid format. 28/02/2014 would work too but if you want it to be identified as dd mm yyyy, you should replace '/' with '-', so that it becomes "28-02-2014". I hope strtotime("28-02-2014") should give you correct result.

      -Anuj

    • Barrilito van Dijk

      Hi Pramod,

      I think Anuj's remark "dont look for a copy-paste solution" makes good sense here.

      I think we all have given the answers and hints to make it work all ready throughout this post, but you have to play a little with all these variables we have given to make it work for your specific wish.

      For instance in example 1 you are still putting a string where an int timestamp is expected. So, please try to look more in what the code does and what type of fields you have instead of just noting that the outcome is not correct.

      I hope you can fix the issue by now, please let us now!

      Thanks!

      Best regards!

    • Pramod Vasudeva Murthy

      Hi Barrilitto and Anuj,

      Yes, I have tried and experimented on this code.

      Please don't be under the impression that I would copy and paste from here and get desired results. We all are part of community and our aim is to help each other and learn.

      I have tried str_replace('/','-',$date_time) and was not yielding desired results.

      I one of the forums I remember reading this:

      There is a difference between '/' and '-' when providing for strtotime(). Dates with '/' will be treated as Object and with '-' is treated as a string not sure if it is true.

      - VIP

    • Barrilito van Dijk

      Hi Pramod,

      Good to hear that you have done your experiments wink

      Perhaps you could look here at this online tool to convert some string to an integer time:

      Setting the date in this tool at 28-02-2014 becomes integer 1393542000.

      Perhaps use such an integer to use in your code to see if the rest of your code works.

      Hope you get it to work Pramod! My comments were meant in a friendly way, I hope you realize that, just wanted to point out that developing is always puzzling with all the seperate pieces laugh

      Regards.

    • Anuj Behl

      Hi Pramod,

      We also want to help you in fixing your issue. Could you let us know the output of below code snippet:

      $datetime="28/02/2014";
      $formatted_date = str_replace('/','-',$datetime)
      $time=strtotime($formatted_date);
      $web_form_1->TestDate = $time;