0

I am trying embed date with particular format by using php code in variable presentdate like below lines of code

var dateFormatPHP = getDateFormat();
var formatToApplyPHP = "";

switch(dateFormatPHP)
{

    case "d-m-Y":
         formatToApplyPHP= "d/m/Y";
    break;

    case "m-d-Y":
        formatToApplyPHP= "m/d/Y";
    break;

    case "Y-m-d":
        formatToApplyPHP= "Y/m/d";
    break;

 }     


var presentdate = <?php echo date(formatToApplyPHP);?>

Please help !!!

Farhad
  • 4,119
  • 8
  • 43
  • 66
  • 2
    `formatToApplyPHP` is a JS variable, you cannot use it in PHP as that runs on the server. Your logic here needs to b entirely moved to either the client or the server, you cannot do a hybrid of the two in the manner you're trying. – Rory McCrossan Oct 13 '17 at 07:36

2 Answers2

0

Javascript data transfer to PHP is usually handle with XmlhttpRequest ,so create php file eg.date.php ! Send that format as param to that file and get response data . Like this example

var dateFormatPHP = getDateFormat();
var formatToApplyPHP = "";

switch(dateFormatPHP)
{

    case "d-m-Y":
         formatToApplyPHP= "d/m/Y";
    break;

    case "m-d-Y":
        formatToApplyPHP= "m/d/Y";
    break;

    case "Y-m-d":
        formatToApplyPHP= "Y/m/d";
    break;

 }     

 $.get("date.php",{d:formatToApplyPHP},function(data) {
      console.log(data);
 });

date.php

<?php
   echo date($_GET['d']);
?>
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
-1

formatToApplyPHP is Javascript variable, try to set this variable using PHP, then you can use this statement:

var presentdate = <?php echo date($formatToApplyPHP);?>
usman ikram
  • 461
  • 4
  • 10