Announcement

Collapse
No announcement yet.

Change the date display format for custom variables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Change the date display format for custom variables

    I expect this has been covered before but seeing as "Date" appears in every post even aided by Google I can't find anything.

    I have a date which is saved for every product as a custom variable.
    When the variable is placed in a layout it appears in the format "9/12/15".
    What I would like is to display it in the format "9 Dec 2015" or "9 December 2015".

    Is there a simple way of achieving this?

    #2
    Assuming your variable is called TESTDATE:
    Code:
    <actinic:block php="true">
        echo date('jS F Y', strtotime(str_replace('/', '-', '<actinic:variable name="TESTDATE" selectable="false" />')));
    </actinic:block>
    See http://www.w3schools.com/php/func_date_date.asp to fiddle with the format string.
    Norman - www.drillpine.biz
    Edinburgh, U K / Bitez, Turkey

    Comment


      #3
      Here's a web-page that lets you generate some popular date format strings or create a bespoke one without knowing coding.

      So just replace the 'jS F Y' above with the string from the bottom of the phpformatedate.com page. E.g. if you see:
      Code:
      echo date('d.m.y');
      then use the red bit.
      Norman - www.drillpine.biz
      Edinburgh, U K / Bitez, Turkey

      Comment


        #4
        Solved - partly

        Your first post was almost exactly what I needed - I just had to change 'jS F Y' to 'j F Y' as I don't want 'st/nd/rd/th' after the day number.

        Many thanks - very helpful.

        EDIT:
        Except, I now have a new problem. It looks like Sellerdeck stores the date in British format, but that the php is expecting it to be US format. So it gets some dates the wrong way round e.g. '10 February' displays as '2 October', and any day past 12 breaks the code and it just displays as '1 January 1970', e.g. '22/7/15' comes out as '1 January 1970'.

        So as Sellerdeck stores the date with slashes '/' strtotime expects it to be in ISO format, i.e. MM/DD/YYYY.

        Comment


          #5
          Solution

          The problem is only that to php a slash means it sees it as US-format e.g. 07/22/2015
          Whereas if dashes are used the date is seen as British/international (proper) format, e.g. 22-7-2015.

          So to use strtotime all we have to do is change the slashes to dashes, which with some help from this post on stackoverflow.com I managed to do http://stackoverflow.com/questions/2...m-yyyy-format:

          The full code which works for me in Sellerdeck is therefore:
          Code:
          	
          <actinic:block php="true">
          $date = '<actinic:variable name="TESTDATE" selectable="false" />';
          	$date = str_replace('/', '-', $date);
          	echo date('j F Y', strtotime($date));
          </actinic:block>
          If you need a different format look here: http://www.w3schools.com/php/func_date_date.asp

          Thanks for your help Norman - don't think I'd have found this on my own!

          Comment


            #6
            Post #2 amended to replace those pesky / chars with - before calling the strtotime routine.
            Norman - www.drillpine.biz
            Edinburgh, U K / Bitez, Turkey

            Comment

            Working...
            X