JKG Posted July 25, 2011 Share Posted July 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242723-date-yyyy-mm-dd-to-javascript-date-yyyymmdd/ Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242723-date-yyyy-mm-dd-to-javascript-date-yyyymmdd/#findComment-1246704 Share on other sites More sharing options...
codebyren Posted July 25, 2011 Share Posted July 25, 2011 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242723-date-yyyy-mm-dd-to-javascript-date-yyyymmdd/#findComment-1246705 Share on other sites More sharing options...
JKG Posted July 25, 2011 Author Share Posted July 25, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/242723-date-yyyy-mm-dd-to-javascript-date-yyyymmdd/#findComment-1246746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.