我花了很多天才解决这个问题,但我无法修复它,并且正在寻求帮助。 可以说我有这个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个提交的buttoms提交日期
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
,但如果我尝试创建如下所示的特定日期,它就像{{1}的魅力一样}。
$func_date
我甚至将function _weeks($date){
$bdate = "2012-03-01";
置于函数内部,但这也无效。
我怎样才能做到这一点?谢谢!
答案 0 :(得分: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::createFromFormat
和mysqli
可以使用
$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) );
}
如果您有任何其他问题,请告诉我