2

私はこの問題に何日も費やしましたが、それを修正することができず、助けを求めています。私がこのphp-sqlを持っているとしましょう:

$sql2= mysql_query("SELECT * FROM jos_comprofiler WHERE id = ".$userid."");
$row2 = mysql_fetch_array($sql2) ;
$bdate= $row2['cb_birthday']; // 2012-03-01 year/month/day

そしてこの関数:

function _weeks($date) { 
  global $bdate ;
  $month = substr($date, 0, 2) ;
  $day = substr($date, 3, 2);
  $year = substr($date, 6, 4) ;
  $bmonth = substr($bdate, 5, 2) ;
  $bday = substr($bdate, 8, 2);
  $byear = substr($bdate, 0, 4) ;
  $birthday = gmmktime(0, 0, 0, $bmonth, $bday, $byear);
  $eventdate = gmmktime(0, 0, 0, $month, $day, $year);

  $weeknumber = (int)(($eventdate - $birthday) / (7 * 24 * 60 * 60)) + 1 ;
  return $weeknumber ;
}

そして私は日付を提出するために10の提出ボタンを持っています

if (isset($_POST['submit0'])){
  if ($userid !=0 ){
    $date0 = $_POST['date0'];
    $func_date= _weeks($date0);
    mysql_query(" UPDATE data SET w_dd = '".$func_date."' WHERE id_user= '".$userid."' ");
    $sql= mysql_query("SELECT id_user FROM data WHERE id_user='".$userid."' ");
    $result= mysql_num_rows($sql);
    mysql_query(" UPDATE data SET dd = '".$date0."' WHERE id_user= '".$userid."' ");
  }

  if (isset($_POST['submit1'])){
    if ($userid !=0 ){
      $date1 = $_POST['date1'];
      $func_date= _weeks($date1);
      mysql_query(" UPDATE data SET w_dd = '".$func_date."' WHERE id_user= '".$userid."' ");
      $sql= mysql_query("SELECT id_user FROM data WHERE id_user='".$userid."' ");
      $result= mysql_num_rows($sql);
      mysql_query(" UPDATE data SET dd = '".$date1."' WHERE id_user= '".$userid."' ");
    }

...以下同様にsubmit9ボタンまで。

問題は$bdate関数にあり、そのようには機能しません$bdateが、以下のように特定の日付を作成しようとすると、チャームのように機能し$func_dateます。

function _weeks($date){
  $bdate = "2012-03-01";

内部関数も入れました$bdateが、それもうまくいきませんでした。どうすればこれを機能させることができますか?ありがとう!

4

1 に答える 1

2

これを試して

$userid = ""; // Nuot sure where this is form ;
$date = "";
$mysql = new mysqli ( "localhost", "root", "", "db" );

if ($mysql->connect_errno) {
    printf ( "Connect failed: %s\n", $mysql->connect_error );
    exit ();
}

$result = $mysql->query ( sprintf ( "SELECT cb_birthday FROM jos_comprofiler id = '%s'", $userid ) );

if (! $result) {
    die ( "Error Runing jos_comprofiler query" );
}

$josProfile = $result->fetch_assoc ();

$_POST ["date0"] = "03/22/2012 "; // Sample
$_POST ["submit0"] = "oK"; // Sample

for($i = 0; $i < 10; $i ++) {
    if (! isset ( $_POST ["submit$i"] ) || ! isset ( $_POST ["date$i"] )) {
        continue;
    }
    $date = $_POST ["date$i"];
    $weekNumber = weekNumber ( $josProfile ['cb_birthday'], $date );
    if ($userid != 0) {
        $this->query ( sprintf ( "UPDATE data SET w_dd = '%s' ,  dd = '%s'  WHERE id_user= '%s' ", $weekNumber, $date, $userid ) );
    }
}

function weekNumber($birthDay,$postDate) {
    $postDate = trim($postDate);
    $birthDay = trim($birthDay);
    $postDate = DateTime::createFromFormat('m/d/Y', $postDate);
    $birthDay = DateTime::createFromFormat('Y-m-d', $birthDay);
    $sec = (int) $postDate->format("U") - $birthDay->format("U");   
    return ceil($sec / (60 * 60 * 24 * 7));
}

問題が発生していてDateTime::createFromFormatmysqli使用できます

$userid = ""; // Nuot sure where this is form ;
$date = "";

$result = mysql_query( sprintf ( "SELECT cb_birthday FROM jos_comprofiler id = '%s'", $userid ) );
$josProfile = mysql_fetch_array($result);

$_POST ["date0"] = "03/22/2012 "; // Sample
$_POST ["submit0"] = "oK"; // Sample

for($i = 0; $i < 10; $i ++) {
    if (! isset ( $_POST ["submit$i"] ) || ! isset ( $_POST ["date$i"] )) {
        continue;
    }
    $date = $_POST ["date$i"];
    $weekNumber = weekNumber ( $josProfile ['cb_birthday'], $date );
    if ($userid != 0) {
       mysql_query( sprintf ( "UPDATE data SET w_dd = '%s' ,  dd = '%s'  WHERE id_user= '%s' ", $weekNumber, $date, $userid ) );
    }
}




function weekNumber($birthDay, $postDate) {
    $postDate = trim ( $postDate );
    $birthDay = trim ( $birthDay );

    list($postMonth,$postD,$postYear) = explode("/", $postDate);
    list($birthYear,$birthMonth,$birthD) = explode("-", $birthDay);

    $postTime = mktime(0, 0, 0, $postMonth, $postD, $postYear);
    $birthTime =  mktime(0, 0, 0, $birthMonth, $birthD, $birthYear);

    $sec = ( int )$postTime - $birthTime;
    return ceil ( $sec / (60 * 60 * 24 * 7) );
}

他に問題がある場合はお知らせください

于 2012-04-10T19:01:34.657 に答える