• I have a php array of dates ($fdates) which outputs as
    array (size=1) 0 => string '2013/08/23' (length=10)

    I need to change this to a Javascript array in this format

    var array = ["2013-09-14","2013-09-15","2013-09-16"]

    using json_encode i get ["2013V09V14"]

    How do i change the format into the format above (its for beforedate in jquery date picker..

    <?php
    var_dump( $fdates ); // test query
    echo json_encode($fdates);
    
    ?>
    <script type="text/javascript">
    var array = <?php echo json_encode($fdates); ?>;
    
    jQuery(document).ready(function() {
        jQuery('#MyDate').datepicker({
    		beforeShowDay: function(date){
            var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
            return [ array.indexOf(string) == -1 ]
        }
        });
    });
    
    </script>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The JSON result you are getting is actually ["2013\/09\/14"] where the slashes from the original date string are escaped with a backslash. If you really need hyphens between the dates, either setup $fdates with them to begin with or use str_replace() to replace all slashes with hyphens.

    If you want a JSON string with 3 dates, you need to feed json_encode() an array containing those 3 dates, right now, your examples do not correlate.

    Thread Starter everyeurocounts

    (@everyeurocounts)

    thanks mate for your answer ??

    sorry information was kinda generic i guess….$fdates is a variable from a database query.

    Hyphens or slashes work ok with beforeshow day (i ended up using implode to string and a php insert into the javascript to create the array with slashes) i’ve included the code below if it helps anyone, just i was wondering if the json_encode could be used effectively for date arrays…

    $comma_seperated = implode('","', $fdates);
    <script type="text/javascript">
    			var array = ["<?php echo $comma_seperated; ?>"];
    
    				jQuery(document).ready(function() {
    				jQuery('#MyDate').datepicker({
    				beforeShowDay: function(date){
    				var string = jQuery.datepicker.formatDate('yy/mm/dd', date);
    				return [ array.indexOf(string) == -1 ]
    				}
    				});
    			});
    
    			</script>
    Moderator bcworkz

    (@bcworkz)

    JSON is just a data exchange format, one of many. As long as the receiver knows how to handle the data, it will work fine. JSON does not explicitly understand dates, it only sees dates as either a date stamp real or a string, depending on the source. As such, the data will transmit accurately. It’s up to the receiver to do what’s necessary. It could remain a string or real or become a SQL date again, it depends on how the data is handled on receipt.

    Short answer is it depends ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change php date array to javascript array’ is closed to new replies.