Jump to content

Recommended Posts

How do I get a range of dates between the start and end dates using vue?

My jsfiddle is as follows: i need to get the proper range of dates instead of the hard coded values in my jsfiddle.

Any help is appreciated.

https://jsfiddle.net/4b0z1exr/3/

 

 

 

Edited by beginnerForPHP

I'd use PHP to generate the dates via an AJAX request, putting the returned range into the JS dateRange.

<?php
    if (isset($_GET['ajax'])) {
        if ($_GET['ajax']=='daterange') {
            $d1 = new DateTime($_GET['start']);
            $d2 = new DateTime($_GET['end']);
            $d2->modify('+1 day');
            $interval = new DateInterval('P1M');
            $dp = new DatePeriod($d1, $interval, $d2);
            
            foreach ($dp as $d) {
                $dstr = $d->format('Y-m-d');
                $dates[$dstr] = ['period'=>$dstr];
            }
            $res = json_encode($dates) ;
            exit ($res);
        }
    }
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Sample</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type='text/javascript'>
    var dateRange
    
    function getRange()
    {
        let start = $("#start_period").val()
        let end = $("#end_period").val()
        $.get(
            "",
            {"ajax":"daterange", "start":start, "end":end},
            function(resp) {
                dateRange = resp
            },
            "JSON"
        )
    }
</script>
</head>
<body>
    <div>
        <input id="start_period" type="date">
        <input id="end_period" type="date">
        <button onclick="getRange()">Click Me!</button>
    </div>

</body>
</html>  

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.