0

So what i'm looking for is a standard countdown for a specific date.

$thepostid = get_the_ID();
$sale_price_dates_to    = ( $date = get_post_meta( $thepostid, '_sale_price_dates_to', true ) ) ? date_i18n( 'd-m-Y', $date ) : '';
?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<p>DATUM: <?php echo $sale_price_dates_to;  ?> </p>

<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Sep 5, 2018").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>

this is what i've got, if i echo $date, is shows a weird long number.

So what i want to try is that $date comes into

var countDownDate = new Date("Sep 5, 2018").getTime();

Now i just say, countdown to sep 5 2018, but i want to insert my $date into that.

My $date echo's into 1450831902, something like that, how can i split that into days months and years?

Greetz William

Milo
  • 3,365
  • 9
  • 30
  • 44
  • Use php date function with format "M d, Y" and put that value in your javascript code – Nitin Bohra Mar 05 '18 at 13:33
  • The first part of the code allready gets a date from the end of the discount date of a woocommerce product from wordpress. The it shows like this : 12-03-2018 . so i want a counter to that date – Willem Munts Mar 05 '18 at 13:38
  • change your code var countDownDate = new Date("Sep 5, 2018").getTime(); to var countDownDate = new Date(" – Nitin Bohra Mar 05 '18 at 13:40
  • Yea that works for me! Thank you very much! – Willem Munts Mar 05 '18 at 13:46
  • @NitinBohra—if that was an answer I'd down vote it. Parsing of strings with the built-in parser is extremely unreliable. The values should be passed directly, e.g. `new Date(2018, 8, 5)` for 2018-09-05. – RobG Mar 05 '18 at 23:02
  • Note that not all days are 24 hours long where daylight saving is observed, so using `(1000 * 60 * 60 * 24)` for one day will be inaccurate, occasionally. See [*Difference between two dates in years, months, days in JavaScript*](https://stackoverflow.com/questions/17732897/difference-between-two-dates-in-years-months-days-in-javascript) and [*How to get the difference of two dates in mm-dd-hh format in Javascript*](https://stackoverflow.com/questions/35504942/how-to-get-the-difference-of-two-dates-in-mm-dd-hh-format-in-javascript/35520995?s=2|85.8078#35520995). – RobG Mar 05 '18 at 23:05
  • Yes @RobG you are right, so I put the my opinion in comment instead of answering to question – Nitin Bohra Mar 06 '18 at 11:41
  • @RobG I can't use direct values. The dates are allready calculated in the back end of wordpress, it has to do with the fact that the client can choose a date that the sale has to last to. If he/she chooses that date, it has to show that date until the sale is available – Willem Munts Mar 06 '18 at 13:13

0 Answers0