Jump to content

Date (YYYY-MM-DD) to Javascript Date (YYYY,MM,DD)


JKG

Recommended Posts

Hey,

 

I wonder if anyone could help me with this.

 

I need to convert a date in the format YYYY-MM-DD to Javascript dates.

Problem is the Javascript dates are 1 month behind and don't contain zero's so it would be:

 

PHP: 2011-01-01 //1st Jan 2011

JAVASCRIPT: 2011,0,1 //1st Jan 2011

 

does anyone know of a function that could help with this?

 

thanks so much for your help.

There is probably a better way.. This is kind of manipulating the date function / mktime in a way it wasn't really meant to be used.

 

<?php
$date="2011-01-01";
$jsdate=date("Y-G-j", mktime(substr($date, 5,2)-1,1,0, substr($date, 5, 2), substr($date, -2), substr($date, 0, 4)));
echo $jsdate;
?>

Any reason you need to do the conversion in PHP?  You could do this all in JavaScript:

 

var date_string = "2011-01-01" ;  // The date printed using PHP
var d = new Date(date_string);
var new_date = d.getFullYear() + ',' + d.getMonth() + ',' + d.getDate();
alert(new_date);

 

Otherwise in PHP you could do something like:

 

<?php
$php_date = '2011-01-01';
$date_parts = explode('-', $php_date);

$js_year = $date_parts[0];
$js_month = (int) $date_parts[1] - 1;  // the (int) will get rid of any leading '0'
$js_day = (int) $date_parts[2];

$js_date = "$js_year,$js_month,$js_day";
echo $js_date;
?>

thanks guys.

i used a combination.

i was using php so loop through the dates to insert them into a javascript calendar.

 

this is my first draft, using the epoch calendar. [http://jkgo.co.uk/epoch]

 

<link rel="stylesheet" type="text/css" href="epoch_styles.css" />
<script type="text/javascript" src="epoch_classes.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
var	ms_cal;      
window.onload = function () {
ms_cal  = new Epoch('epoch_multi','flat',document.getElementById('multi_container'),true);
<?php 
if(isset($_POST['d'])){
$i = 1;
foreach($_POST['d'] as $val){
    	$date = $val . '';
    	$iplus = $i++;
	$jsdate=date("Y,G,j", mktime(substr($date, 5,2)-1,1,0, substr($date, 5, 2), substr($date, -2), substr($date, 0, 4)));
	echo 'var date'.$iplus.' = new Date('. $jsdate . ')
	date'.$iplus.'.type = "unavailable";
	';
}

echo 'var myArray = new Array(';
$i2 = 1;
while($i2<=$iplus){
  		echo "date".$i2++;
  		if($i2 <= $iplus){
  			echo ',';
  		}
}

echo ');
ms_cal.addDates(myArray);';
}
?>
		};

/*]]>*/

</script>

<form action="" method="post" onsubmit="ms_cal.sendForm(this,'d');">
<div id="multi_container"></div>
<input type="hidden" name="hidden"/>
<div><input type="submit" value="Mark selected dates as Unavailable"/></div>
</form>

 

thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.