SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Date for fines integer

    Greetings!

    I'm trying to make a formula that computes fine charge in my project.
    Here is my simple code.
    PHP Code:
     $dues =  date_create($row['Datedue']);
     
    $duedate_format($dues,'Ymd');
     
    $now =  date("Ymd");
     
    $cost $row['Fines'];
     
    $fine $now-$due;
                                        
                if (
    $fine<=0){
                
    $fines 0;
                }else {
                           
    $fines $fine $cost;
                             } 
    My problem is I want to exclude the saturday and sunday in computing. Is that possible?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    So if it's Sun through Sat between $due and $now, You'd want it to be 5 * $cost?

    I would simply iterate from $now till $due, and use date() to check if it's a saturday/sunday and skip it.

    You could also do it mathematically based on the day $now is versus the day $due is, using modulus and some if condition checks, but I would just go with the first suggestion to make it easier on yourself and future maintainability.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thank you for quick response.I'm not sure if I get it.

    Assuming that I borrowed books from our library, and should be returned
    after 6 days (excluding sat and sun), beyond that I must pay book fines with $1/day.

    What do you mean by 5 * cost. ?
    Hope I'm making my statement clear.

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What does $row['Datedue'] look like?

  5. #5
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Its a date("Y-m-d") format.

    20120303 - 20120229 = 3 * fines.
    Now - Due = 3 * fines.

  6. #6
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        $due =  strtotime($row['Datedue']);
        
    $now time();
        
    $cost $row['Fines']; 
        
    $days 0;
        
        while (
    $now $due)
        {
            if (
    date('N'$now) == || date('N'$now) == 7) continue; # skip sat/sun
            
    $days++;
            
    $now strtotime('+1 Day'$now);
        } 

  7. #7
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    there is an error:

    Code:
    Fatal error: Maximum execution time of 60 seconds exceeded
    What does it mean?

  8. #8
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    That means that loop is causing an infinite loop.. hmm..

    I see the error in logic.

    PHP Code:
        $due =  strtotime($row['Datedue']);
        
    $now time();
        
    $cost $row['Fines']; 
        
    $days 0;
        
        while (
    $now $due)
        {
            if (!
    date('N'$now) == && !date('N'$now) == 7) {
                
    $days++;
            }
            
    $now strtotime('+1 Day'$now);
        } 
    It was basically getting caught because if it was a saturday, it would start the loop over, then $now would still be saturday.

  9. #9
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the code, but can you explain or just tell something about your code? I'm new in php.
    Where can I put my formula in finding the product of fines?
    like now-due date = fines. How about the interval of book when borrowed? like when I borrowed book that should be returned after 3 days.

  10. #10
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    My code replaces all of yours.

    If you don't understand my code, which I was hoping you would, then simply add this after the whole loop.

    $fines = $days * $cost;

  11. #11
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,335
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    @wonshikee ; You applied a very elegant solution to this problem. I have been away from PHP for a while and was struggling (mentally) between the two different approaches I would have taken.
    Your code is concise, clean and effective!
    :applause:
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  12. #12
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ParkinT View Post
    @wonshikee ; You applied a very elegant solution to this problem. I have been away from PHP for a while and was struggling (mentally) between the two different approaches I would have taken.
    Your code is concise, clean and effective!
    :applause:
    Thank you sir.

  13. #13
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I made this code to understand my problem.

    PHP Code:
    $query mysql_query("SELECT * FROM tbl_studentbook WHERE id = 3") or die (mysql_error());
        while (
    $row mysql_fetch_array($query))
        {
            
    $bor =   date_create($row['sbook_Borrow']);
            
    $bordate_format($bor,'N'); //2012-03-03 sat
            
            
    $due =   date_create($row['sbook_Due']);
            
    $duedate_format($due,'N'); //2012-03-18 sun
            
    $cost 5;
            
            if (
    $due == 6){ 
            
    $due $due 2;
            }if (
    $due == 7){
            
    $due $due 1;}
            
            
    //my new due date should be 2012-03-19 monday
            
            
    $now date();//assuming today is 2012-03-21 wed
            
            //so I must pay 2 days book fines which cost $1
            
    $fines $now $due;
            
    $fines $cost;
            
    $totalfines 10;
        } 
    Help me more please. thank you.

  14. #14
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Here's a little function that takes the due date and current date, and (should) return the number of days overdue.

    PHP Code:
    function days_overdue(DateTime $dueDateTime $now)
    {
        
    // Skip to next weekday if on weekend
        
    if (in_array($due->format('D'), array('Sat''Sun'))) {
            
    $due = clone $due;
            
    $due->modify('next weekday');
        }

        
    // Generate period of weekdays between start and end
        
    $period = new DatePeriod($dueDateInterval::createFromDateString('next weekday'), $nowDatePeriod::EXCLUDE_START_DATE);

        return 
    iterator_count($period);

    Your code snippet would then look like

    PHP Code:
        while ($row mysql_fetch_array($query)) 
        {
            
    // ...

            
    $due date_create($row['sbook_Due']); 
            
    $now date_create();

            
    $overdue days_overdue($duedate_create());
     
            
    // so I must pay $overdue days book fines which cost $cost dollars per day
            
    $cost 5
            
    $fines $overdue_days $cost;

            
    // ...
        

    See also
    Salathe
    Software Developer and PHP Manual Author.

  15. #15
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Than you for the code.

    When I tried to echo the $due it says an error lilke this. $fines also is 0.

    PHP Code:
    Catchable fatal errorObject of class DateTime could not be converted to string in 

  16. #16
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The links helps me a lot. Thank you very much.
    I think I understand more in procedural programming.
    I made this code. Not that elegant but effective.

    $cost = 5;
    $dues = date_create($row['sbook_Due']);
    $due= date_format($dues,'N');
    $now = date('Ymd');

    if ($due == 6)
    {
    $dues = date_create($row['sbook_Due']);
    $new = date_modify($dues, '+2 day');
    $newdue = date_format($new, 'Y-m-d');
    }
    if ($due == 7)
    {
    $dues = date_create($row['sbook_Due']);
    $new = date_modify($dues, '+1 day');
    $newdue = date_format($new, 'Y-m-d');
    }


    echo $newdue;

  17. #17
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    A problem with that is that $newdue will not exist if $dues falls on a weekday.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •