php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75524 last monday this week working wrong
Submitted: 2017-11-14 16:44 UTC Modified: 2017-11-15 03:27 UTC
From: padre dot cedano at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.0.25 OS:
Private report: No CVE-ID: None
 [2017-11-14 16:44 UTC] padre dot cedano at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/datetime.modify
---

If i try this today, 2o17-11-14:

modify('last monday this week')

the result is:

06/11/2017

But last monday this week was:

13/11/2017



Test script:
---------------
<?php 

    function getLastDayofWeek ($strDia){
        $date=new DateTime('now');
        $strCriterio='last '.$strDia.' this week';
        $date->modify($strCriterio);
        echo $strCriterio.": ".$date->format('d/m/Y').PHP_EOL;
    }

    /*Pruebas*/

    getLastDayofWeek('monday');
    getLastDayofWeek('saturday');
    getLastDayofWeek('sunday');

?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-14 16:45 UTC] padre dot cedano at gmail dot com
-Package: PDO Core +Package: Date/time related
 [2017-11-14 16:45 UTC] padre dot cedano at gmail dot com
Previuos package error
 [2017-11-14 17:04 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2017-11-14 17:04 UTC] danack@php.net
I really doubt this is a bug. 

Doing 'monday this week' is not only clearer english, but also gives you the result you want, I think.

https://3v4l.org/UYsNE

'last monday this week' - means from the start of this week, find the previous monday. Not find the monday of this week.
 [2017-11-15 03:27 UTC] padre dot cedano at gmail dot com
Ok. My function is working as:

    function getLastDayofWeek ($strDia){
        $date=new DateTime('now');
        $strCriterio='last '.$strDia;
        $d=$date->modify($strCriterio);
        echo $strCriterio.":\t ".$d->format('d/m/Y').PHP_EOL;
    }

Result:
    last monday:	 13/11/2017
    last tuesday:	 14/11/2017
    last wednesday:	 08/11/2017
    last thursday:	 09/11/2017
    last friday:	 10/11/2017
    last saturday:	 11/11/2017
    last sunday:	 12/11/2017

But is not working if i pass the $date as parameter:

    function getLastDayofWeekFake ($strDia,$date){
        $strCriterio='last '.$strDia;
        $d=$date->modify($strCriterio);
        echo $strCriterio.":\t ".$d->format('d/m/Y').PHP_EOL;
    }

Result:

    last monday:	 13/11/2017
    last tuesday:	 07/11/2017
    last wednesday:	 01/11/2017
    last thursday:	 26/10/2017
    last friday:	 20/10/2017
    last saturday:	 14/10/2017
    last sunday:	 08/10/2017

Why is not working in second function?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 02:01:29 2024 UTC