2

I have spent many days on this problem but I couldn't fix it, and am looking for help. Lets say I have this 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

and this function:

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 ;
}

and i have 10 submit buttoms to submit dates

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."' ");
    }

...and so on till submit9 button.

The problem is $bdate in the function , it doesn't work $bdate like that, but if I try to make a specific date like below, it works like a charm the $func_date.

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

I even put the $bdate inside function but that didn't work either. How can I make this work? Thanks!

9
  • Can you echo $bdate and see whether the date format is okay? Commented Apr 10, 2012 at 17:55
  • if i echo $bdate alone without function , its echoing the date normal its good. Commented Apr 10, 2012 at 18:00
  • echo it inside the function call so you can see what value it gets Commented Apr 10, 2012 at 18:00
  • 1
    You should also really pass in $bdate to the function as an argument as well. global is evil in almost all cases. Commented Apr 10, 2012 at 18:05
  • can you explain litle prodigitalson ? Commented Apr 10, 2012 at 18:08

1 Answer 1

2

Try this

$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));
}

You you are having issue with DateTime::createFromFormat and mysqli you can use

$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) );
}

Let me know if you have any additional issues

Sign up to request clarification or add additional context in comments.

21 Comments

i want calculate number of weeks ($weeknumber) when date is submitted ._weeks is just a name of function. if user submit a date , this function will calculate the number of weeks from this date and then store it in database ($func_date), i just want say that i have many dates to be submitted as shown up from date0 to date9
One clarification ... when you mean ` the number of weeks from this date ` do you mean week number form that date till now .. or week number of the current date in the year
thx for trying . week number i mean between his birthday and the date of this submitting. $date (which he submitt) - $birthday
Cool .. sorry i question is too much .... also clarify .. are you calculating week number since he was born ? or just the current year .. (If its the current .. year .. what if his birthday has passed ?? )
its this in my code up $weeknumber = (int)(($eventdate - $birthday) / (7 * 24 * 60 * 60)) + 1 ;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.