-1

I have a PHP variable which is a date format string, it is "Y-m-d" but could be other date format strings, I'd like to pass this to a script which requires a JS date format string.

Therefore my Y-m-d from PHP is the equivalent of YYYY-MM-DD in JS.

What can I do on either the PHP or JS side to convert the PHP based date format string of Y-m-d to the JS equivalent?

I am looking to convert the date format string itself, not the actual date.

bigdaveygeorge
  • 947
  • 2
  • 12
  • 32
  • `var date = ` and just format it from there? Or create a PHP datetime object and format it and store it in a JS variable. There's multiple ways that work – Andy Holmes Dec 04 '20 at 17:32
  • $date would be the string 'Y-m-d' - the format of the date, not the actual date, i'm trying to convert that date string to the JS equivalent date format string. Would datetime object work for that? – bigdaveygeorge Dec 04 '20 at 17:41
  • Forgive me for asking it like this, but, what on earth are you doing? I'm so confused by your situation. Can you maybe update your question to give us an example or properly clarify what it is you're trying to achieve? – Andy Holmes Dec 04 '20 at 17:53
  • The JS date object doesn't have date formatting tokens in the same way as PHP. why do you need to do this anyway? – ADyson Dec 04 '20 at 17:54
  • I am using this https://wakirin.github.io/Litepicker/ it accepts the date format as a string e.g. YYYY-MM-DD, I have the date format saved in a database as Y-m-d (PHP based date format), I just want to pass that date format to the script and thought I might be able to convert it to be in the format it accepts. – bigdaveygeorge Dec 04 '20 at 17:56
  • I'm not sure of the issue though, `Y-m-d` in PHP _is_ `YYYY-MM-DD` in JS (2020-12-04) – Andy Holmes Dec 04 '20 at 18:02
  • Litepicker does not accept the date format string I am trying to use of Y-m-d it must be in the format of YYYY-MM-DD – bigdaveygeorge Dec 04 '20 at 18:07
  • Can you share some actual code of what you're dealing with so we can try and replicate please? – Andy Holmes Dec 04 '20 at 18:08
  • Is there a reason you wouldn't want to hardcode this value? Does your application really have to switch date formats around so much that you want to programatically handle it? – El_Vanja Dec 04 '20 at 18:09
  • Seems like you would need to map all the date format tokens from PHP to all the date format tokens that litepicker accepts (I assume maybe they document this somewhere) and then use a lookup data structure to convert from one to the other. If there are some which don't have a direct mapping then it might get more interesting – ADyson Dec 04 '20 at 18:41
  • The date format is set by the logged in user to how they want the date formatted which maybe different to another user. It sounds like this is far more complex than originally thought and I'll have to look for an alternative solution. – bigdaveygeorge Dec 04 '20 at 19:38
  • Well you can always make it simpler by restricting the formatting tokens that users can specify – ADyson Dec 04 '20 at 23:12
  • ECMAScript doesn't have a date formatter so what you're asking is impossible. There are various libraries that you might be able to use if you parse the PHP format string and create an equivalent set of tokens for the library, or just write your own ECMAScript formatter using PHP tokens, e.g. [*Function to convert PHP date format to Javascript date format (not the date itself !)*](https://stackoverflow.com/questions/57279831/function-to-convert-php-date-format-to-javascript-date-format-not-the-date-itse) which does parsing as well. – RobG Dec 05 '20 at 07:02

1 Answers1

0

I'm really not sure on what your issue is. But I've spun up a local version of what you're using and I've created the following code:

  <input value="<?php echo date('Y-m-d'); ?>" id="litepicker">
  <script src="https://cdn.jsdelivr.net/npm/litepicker/dist/js/main.js"></script>
  <script>
    var picker = new Litepicker({ 
      element: document.getElementById('litepicker'),
      format: 'D MMM, YYYY',
    });
  </script>

As you can see, its a new Litepicker instance, formatted to look nicely, but the input element has the value set to what you'd get back from the database, and it looks like this on screen:

enter image description here

If I'm misunderstanding you, please let me know. But I cannot see the issue.

Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
  • No, i'm effectively wanting to use the PHP date echo in the line: format: 'D MMM, YYYY' But it won't accept the PHP date format string – bigdaveygeorge Dec 04 '20 at 19:39
  • @bigdaveygeorge you're going to have to really explain what isn't working. I'm using php date in the correct way and parsing the date formatting inside the Litepicker instance, I have no clue what your issue is or what you're trying to achieve, it's working correctly. – Andy Holmes Dec 05 '20 at 17:39