converting date formats

Posted on 2011-04-19
Last Modified: 2012-06-21
need a function to convert m-d-Y or Y-m-d

i want to run the function anytime a date variable is used & only modify the variable data IF it fits the format to be modified.

in layman's terms:
if( *date* == format m-d-Y){ convert to Y-m-d )
Question by:Imaginx
    LVL 107

    Accepted Solution

    LVL 107

    Expert Comment

    by:Ray Paseur
    Your question is a little hard to understand because clients have so many different ways of expressing dates.  Consider this: 03-05-2011.  Do you think that should be March 5 or May 3?  Would you want to convert 03-05-2011 but not convert 03/05/2011?  Would you want to not use ISO8601 formats for some reason?  Questions like that are lurking in the design that seems to underpin your question.  If you can give us a little more context and explanation we may be able to give you a better answer.  In the correct computer science methods, you would do something like this:

    $s = '03-05-2011';
    $t = strtotime($s);
    if (!$t) die("$s is a bogus date string");
    $d = date('Y-m-d', $t);
    echo $d;

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now