Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Dealing with really old dates in php

    Hello,

    I need to display dates which fall outside of the usable date range. Any ideas?

    I'll tell you a bit more: The website has several articles about various people, including the dates on which they were born and died, (if they are in fact dead).

    I need to format these dates for display but my testing machine uses windows and therefore the date() function is no use before 1970. I will be using dates as early as the 15- or 1600's so I am assuming that any OS the site is hosted on will not have system support for those dates.

    The dates are stored in a MySQL date field yyyy-mm-dd. Does anybody know how I can format these dates somehow getting around the range limits of the date() function?

    Thanks,

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have a look at the calendar functions
    http://us2.php.net/manual/en/ref.calendar.php

    Maybe this PEAR class supports editing of old dates too
    http://pear.php.net/package/Date

    It depends, however, what you actually want to do with the dates. Complex date arithmetic or just retrieving some info like the name of day of birth? The links above should help you get started, though.
    De gustibus non est disputandum.

  • #3
    piz
    piz is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Barcelona
    Posts
    246
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use some RegExp like this one:

    PHP Code:
    $MySQLDate "15501122133055"//just a sample MySQL Date String
    $mdy preg_replace("/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]*)$/""$2. $3. $1"$MySQLDate); 
    Greetz
    piz

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't see the problem.

    date() is used to create/format a date, right? So what is your actual problem?

    You get the date as a string with a fixed structure (YYYY-MM-DD), and you want to turn it into a string with another structure (MM-DD-YYYY or MM/DD/YYYY or whatever) Has got nothing to do with dates, as far as i understand it
    So all you need are a few (1) stringfunctions. Like
    PHP Code:
    $datefromdb '1568-01-18'
    $newdate substr($datefromdb, -2)  . '/' substr($datefromdb5,2) . '/' substr($datefromdb,0,4) ; 
    $newdate then contains 18/01/1568

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    or better still use MySQL date formatting functions to format the dates before they get out of the DB , eg

    "SELECT DATE_FORMAT(date , '%W %M %Y' ) as formatted_date FROM $table"

    thats assuming your dates are within the MySQL allowed range from the year 1000 , to the year 9999
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    So far firepages wins! I'm looking for the easiest way to format the date, and working with regular expressions or a bunch of string functions, while they work, are not really that fun.

    There is a good chance I will have to do some date arithmatic in the future as well, and the MySQL date functions will take care of that, and all of my dates are within the MySQL range.

    Thanks!

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #7
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mySQL has built in functions to extract months, dates, year, hours, minutes and seconds from a date, time, or datetime field.
    here is an example query:
    Code:
    SELECT birthdate
    MONTH(birthdate) AS birthmonth, DAYOFMONTH(birthdate) AS birthday
    FROM meets
    this goes along with what firepages said. personal preference, i guess.


  •  

    Posting Permissions

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