Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

[Question] PHP strtotime query

  • 27-01-2011 10:58am
    #1
    Registered Users, Registered Users 2 Posts: 601 ✭✭✭


    Hi,

    I want to be able to use to strtotime method to perform various date changes, e.g. previous sunday, next saturday, previous month, etc.
    Below line returns 25/12/1969
    $startYearPrev = date('d/m/Y', strtotime('-1 week', "01/01/2011"));

    however
    print date('d/m/Y', strtotime('-1 week'));
    returns 20/01/2011 which is correct.

    strtotime takes in the date as an int, right?
    So how do I convert a string in the format "dd/mm/yyyy" to an int?
    Or is there a better way?
    Cheers


Comments

  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    The second argument needs to be a valid date integer.

    What you will need to do is extract the value of the text date as an integer. This is also what strtotime does.

    So the below will do what you're looking for:

    date('d/m/Y', strtotime('-1 week', strtotime("01/01/2011")));


  • Registered Users, Registered Users 2 Posts: 601 ✭✭✭garlad


    Seamus,
    Works perfect.
    Thanks a million.


  • Registered Users, Registered Users 2 Posts: 601 ✭✭✭garlad


    Doing some testing on this:


    $startYearSelected = "01/01/2011";
    $endYearSelected = "31/12/2011";

    print date('d/m/Y', strtotime('-1 year', strtotime($startYearSelected)));
    displays 01/01/2010, correct

    print date('d/m/Y', strtotime('-1 year', strtotime($endYearSelected)));
    displays 01/01/1969 , incorrect. Expecting 31/12/2010

    Any ideas?


  • Registered Users, Registered Users 2 Posts: 601 ✭✭✭garlad


    Works if I replace "/" with ".":

    $startYearSelected = "01/01/2011";
    $endYearSelected = "31/12/2011";

    print date('d/m/Y', strtotime('-1 year', strtotime(str_replace('/', '.', $startYearSelected))));
    Displays 01/01/2010, correct

    print date('d/m/Y', strtotime('-1 year', strtotime(str_replace('/', '.', $endYearSelected))));
    Displays 31/12/2010, correct

    Weird


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    strtotime can be a bit hit-and-miss because a lot of the time you're accepting text inputs and you can have any and all formats of dates.

    Quick test for your script.

    Try
    print date('d/m/Y', strtotime('-1 year', strtotime("01/02/2010")));

    If it outputs 02/01/2009 then it's reading in dates with the US locale, hence why 31/12/2011 is being read as an invalid date (it thinks you're trying to say 12th of 31st month, 2011)

    edit:

    Actually if you look at the date format list for this function, dd/mm/yyyy is not a valid date format for strtotime, only mm/dd/yyyy is.

    However, as you've discovered, dd.mm.yyyy is a valid date format for strtotime.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 601 ✭✭✭garlad


    Yep, it outputs 02/01/2009 so must be using a US locale.
    The str_replace() seems to be the answer though


  • Registered Users, Registered Users 2 Posts: 2,345 ✭✭✭Kavrocks


    Instead of using strtotime so often why don't you use mktime()? It could prove to be easier to debug.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Kavrocks wrote: »
    Instead of using strtotime so often why don't you use mktime()? It could prove to be easier to debug.
    I also found unix time stamps easier to work with as you can do easy arithmetic on them. Then convert back later.


Advertisement