2013-09-02 2 views
0

임 내가 전에 PHP에서 수행 한 JS를 통해 yyyy-mm-dd의 형식으로 날짜를 전달하는 방법을 알아 내려고하지만, PHP와 JS는 이러한 의미에서 다릅니다. 나는 약간의 손실이 있습니다.JS에서 formating PHP date formating

내가 PHP에서 그것을했다 Heres는 방법 :

var default_dob = strtotime(date('m/d/Y', time()) .' -18 year'); 
var dob = date('m/d/Y', default_dob); 

기본적으로 십팔년 뺀, 오늘 날짜를 가지고, 그래서 다시 포맷의 날짜 선택기에 대한 mm/dd/yyyy. Idealy 나는 벌써 큰 JS 스택에 또 다른 플러그인을 추가하는 것을 피하고 싶습니다. 그래서 (아마 준비 만든 기능 아픈 행복에 연결 할 수있는 외부)에 여분의 무게없이

+0

이 사이트 [http://phpjs.org/functions/date/](http://phpjs를 참조하십시오. org/functions/date /) – coolprarun

답변

0

을이 작업을 수행 할 수있는 경우 strtotime의 출력은 자바 스크립트 Date 객체에 전달할 수있는 UNIX 타임 스탬프입니다.

var dob = new Date("<?php echo strtotime('-18 year'); ?>" * 1000); 

그런 다음이 날짜 포맷 :

var dateString = dob.getFullYear() + '-' + (dob.getMonth() + 1) + '-' + dob.getDate(); 

을 또는 날짜 형식으로 Date 객체는, 당신이 할 수 있도록 한 줄에 1000에 의해 그 수를 곱 UNIX의 에폭 (epoch) 이후 밀리 초 (안 초) 가정 선두에 제로 :


주로-PHP 솔루션이다

var dateString = dob.getFullYear() + '-' + ("00" + (dob.getMonth() + 1)).slice(-2) + '-' + ("00" + dob.getDate()).slice(-2); 
date 쿵푸를 사용하는 nction 날짜를 포맷합니다 :

var dateString = "<?php $ts = strtotime('whatever'); echo date('Y-m-d', $ts); ?>"; 
1

이 정확히 십팔년 다시 필요한 형식으로 날짜를 알려줍니다. 또한

var date = new Date(); 
date.setFullYear(date.getFullYear() - 18); 
alert(date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate()); 
0
// Left pad a string to the specified length using the specified character 
function padLeft(str, length, char) 
{ 
    // Make sure args really are strings and that the length is a positive 
    // number. If you don't do this concatenation may do numeric addition! 
    str = String(str); 
    char = String(char) || ' '; // default to space for pad string 
    length = Math.abs(Number(length)); 
    if (isNaN(length)) { 
     throw new Error("Pad length must be a number"); 
    } 

    if (str.length < length) { 
     // Prepend char until the string is long enough 
     while (str.length < length) { 
      str = char + str; 
     } 

     // Make sure the string is the requested length 
     return str.slice(length * -1); 
    } else { 
     // The string is already long enough, return it 
     return str; 
    } 
} 

// Get the current date/time 
// This is local to the browser, so it depends on the user's system time 
var default_dob = new Date(); 

// Subtract 18 years 
default_dob.setFullYear(default_dob.getFullYear() - 18); 

// Format the string as you want it. PHP's d and m formats add leading zeros 
// if necessary, in JS you have to do it manually. 
var dob = padLeft(default_dob.getMonth(), 2, '0') + '/' 
     + padLeft(default_dob.getDate(), 2, '0') + '/' 
     + default_dob.getFullYear() 

관련 항목 : MDN entry on the Date() object

0

<script type="text/javascript"> 
$ss= date('m/d/Y', strtotime('+18 year')); 
?> 
var default_dob = '<?php echo $ss;?>'; 
alert(default_dob); 
</script>