1

Am trying to convert PHP Timestamp date to a format that can be handled by jquery as a target date for count up timer but i cant find a way around. Here is my code; It only works when i manually edit the target date but when i echo PHP Timestamp then it refuses.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="flipTimer/css/flipTimer.css">
<script src="jquery-3.4.1.min.js"></script>
<script src="flipTimer/js/jquery.flipTimer.js"></script>
</head>
<body>
<div class="flipTimer">
<div class="days"></div>
<div class="hours"></div>
<div class="minutes"></div>
<div class="seconds"></div>
</div>
<script>
var date1="<?php echo $time; ?>";
$(document).ready(function(){
$('.flipTimer').flipTimer({
//count up or count down
direction: 'up',
// the target date that works when i edit manually
date:
'25 July, 2020 07:30:30 ',
//The date format that causes an error
date:
'date1',
});
});
</script>
</body>
</html>
  • How is your PHP time format looks like ? – Always Helping Jul 25 '20 at 05:54
  • What about using an EPOCH date instead? It will solve your issue using less code. – NVRM Jul 25 '20 at 10:07
  • @AlwaysHelping the time format is in Y/M/D H:M:S –  Jul 25 '20 at 17:45
  • 1
    @NVRM I dont know about EPOCH –  Jul 25 '20 at 17:45
  • https://en.wikipedia.org/wiki/Epoch_(computing). Right now for example the time is `1595699245` in seconds, or `1595699245000` in milliseconds since the 1st January 1970. This allow to easily share a date and roll math, without messing with the month and such. Then in js do this => https://stackoverflow.com/a/8016205/2494754 Good luck! – NVRM Jul 25 '20 at 17:46

1 Answers1

0

The problem is in this line:

var date1="<?php echo $time; ?>";

You are defining date1 as a string however, when you instantiate a date object with a timestamp it should be an integer. Also remember the php timestamp is going to be in seconds while JavaScript expects milliseconds.
Try this:

var date1 = <?php echo $time; ?>;
// or
var date1 = <?php echo time() * 1000; ?>;

$(document).ready(function(){
  $('.flipTimer').flipTimer({
    direction: 'up',
    date: date1,
  });
});
Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31