0

My code for gettin date in jquery is given below :

 var pathname = window.location.pathname; // Returns path only
            var url      = window.location.href;     // Returns full URL
            var splits   =url.split('?');
            if(splits[1])
            {
                 var splits2   = splits[1].split('=');
                 var newDate   = splits2[1];
            }
            else
            {
               var  date    = new Date(),
                    yr      = date.getFullYear(),
                    month   = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(),
                    day     = date.getDate()  < 10 ? '0' + date.getDate()  : date.getDate(),
                    newDate = yr + '-' + month + '-' + day;

            }

After this i am sending this to php code :

         $.ajax({
                      type:'post',
                      url:'<?php echo BASE_URL; ?>dashboard/get_weedays',
                      async:false,
                      data:{date : newDate},
                      success:function(response)
                      {

                        var obj = JSON.parse(response);                        
                        design_for_week(obj);

                      }
                  })
In php i am getting this date and converting it into strtotime 

but its returning me the different values .In php i am doing this

$date  =  $this->input->post("date");
$dt = strtotime($date);
  echo $dt;exit;

for date 2018-11-06 timestamp in php is : 1541442600

but when i pass the date from the ajax and after getting it on php it return me timestam like : 1541442600

both are different can anyone please suggest me fo this ? how can i solve this 1541615400

Kritika
  • 11
  • 1

2 Answers2

0

Transfer UTC timestamp from client, to avoid timezone difference between client and server.

In javascript get UTC timestamp:

     var pathname = window.location.pathname; // Returns path only
     var url      = window.location.href;     // Returns full URL
     var splits   =url.split('?');
     if(splits[1])
     {
          var splits2 = splits[1].split('=');
          var getDate = splits2[1];
          var datum = Date.parse(getDate); // gets date//
          var newDate =  Math.floor(datum/1000);
          //console.log(newDate);
     }
     else
     {
          var  date   = new Date(new Date().toUTCString());
          var jsmonth = date.getMonth()+1;
          var yr      = date.getFullYear(),
          var month   = jsmonth < 10 ? '0' + jsmonth : jsmonth, //january is 0
          var day     = date.getDate()  < 10 ? '0' + date.getDate()  : date.getDate(),
          var getDate = yr + '-' + month + '-' + day;
          var datum = Date.parse(getDate);
          var newDate =  Math.floor(datum/1000);
          //console.log(newDate);
     }

read more: How do I get a UTC Timestamp in JavaScript?

Manpreet
  • 2,450
  • 16
  • 21
0
var pathname = window.location.pathname; // Returns path only
            var url      = window.location.href;     // Returns full URL
            var splits   =url.split('?');
            if(splits[1])
            {
                 var splits2   = splits[1].split('=');
                 var newDate   = (new Date(splits2[1])).toUTCString();
                 date = new Date(newDate).getTime()/1000;
            }
            else
            {
               var  dates   = (new Date()).toUTCString();
               date = new Date(dates).getTime()/1000;

            }

For this we need to do like this because u need to covert tiem inti utc then divided this from 1000 because its a client time

Kritika
  • 11
  • 1
  • Check my updated answer. You need to get date not time in javascript since you are also getting date from URL. – Manpreet Nov 06 '18 at 06:03