php function to format date's time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    php function to format date's time

    Hi,

    I am retrieving results from an SQL query, one of which is a date

    $arrival = $row['itin_arrival_d ay'];

    I would like to write a PHP function that formats the date in the
    following way:

    If the time part of the date is of the form 2006-05-19 00:00:00 in
    which the time is midnight, I would like the result to be printed as
    "05/19/06 ---" but if the time is anything other than midnight, for
    example, "2006-05-21 18:00:00" I would like the printed result to be
    "05/21/06 6:00 PM".

    How can I do this? - Dave

  • Juliette

    #2
    Re: php function to format date's time

    laredotornado@z ipmail.com wrote:[color=blue]
    > Hi,
    >
    > I am retrieving results from an SQL query, one of which is a date
    >
    > $arrival = $row['itin_arrival_d ay'];
    >
    > I would like to write a PHP function that formats the date in the
    > following way:
    >
    > If the time part of the date is of the form 2006-05-19 00:00:00 in
    > which the time is midnight, I would like the result to be printed as
    > "05/19/06 ---" but if the time is anything other than midnight, for
    > example, "2006-05-21 18:00:00" I would like the printed result to be
    > "05/21/06 6:00 PM".
    >
    > How can I do this? - Dave
    >[/color]


    Untested and probably not the most efficient, but should work:

    $arrival_array = explode( ' ', $arrival );

    // format the date
    $date_array = explode( '-', $arrival_array[0] );
    $arrival_date = $date_array[1] . '/' . $date_array[2] . '/' . (
    $date_array[0] - 2000 );
    unset( $date_array );

    // format the time
    if( $arrival_array[1] === '00:00:00' ) {
    $arrival_time = '---';
    }
    else {
    $time_array = explode( ':', $arrival_array[1] );
    if( $time_array[0] > '12' ) {
    $arrival_time = ( $time_array[0] - 12 ) . ':' $time_array[1] . ' PM';
    }
    else {
    $arrival_time = $time_array[0] . ':' $time_array[1] . ' AM';
    }
    unset( $time_array );
    }

    print $arrival_date . ' ' . $arrival_time;

    Comment

    • ZeldorBlat

      #3
      Re: php function to format date's time


      Juliette wrote:[color=blue]
      > laredotornado@z ipmail.com wrote:[color=green]
      > > Hi,
      > >
      > > I am retrieving results from an SQL query, one of which is a date
      > >
      > > $arrival = $row['itin_arrival_d ay'];
      > >
      > > I would like to write a PHP function that formats the date in the
      > > following way:
      > >
      > > If the time part of the date is of the form 2006-05-19 00:00:00 in
      > > which the time is midnight, I would like the result to be printed as
      > > "05/19/06 ---" but if the time is anything other than midnight, for
      > > example, "2006-05-21 18:00:00" I would like the printed result to be
      > > "05/21/06 6:00 PM".
      > >
      > > How can I do this? - Dave
      > >[/color]
      >
      >
      > Untested and probably not the most efficient, but should work:
      >
      > $arrival_array = explode( ' ', $arrival );
      >
      > // format the date
      > $date_array = explode( '-', $arrival_array[0] );
      > $arrival_date = $date_array[1] . '/' . $date_array[2] . '/' . (
      > $date_array[0] - 2000 );
      > unset( $date_array );
      >
      > // format the time
      > if( $arrival_array[1] === '00:00:00' ) {
      > $arrival_time = '---';
      > }
      > else {
      > $time_array = explode( ':', $arrival_array[1] );
      > if( $time_array[0] > '12' ) {
      > $arrival_time = ( $time_array[0] - 12 ) . ':' $time_array[1] . ' PM';
      > }
      > else {
      > $arrival_time = $time_array[0] . ':' $time_array[1] . ' AM';
      > }
      > unset( $time_array );
      > }
      >
      > print $arrival_date . ' ' . $arrival_time;[/color]

      Alternatively, try something like this:

      function foo($str) {
      $t = strtotime($str) ;
      return $t == mktime(0, 0, 0, date('m', $t), date('d', $t),
      date('Y', $t)) ? date('m/d/y ---', $t) : date('m/d/y g:i', $t);
      }

      Comment

      • laredotornado@zipmail.com

        #4
        Re: php function to format date's time

        Thanks for this neat, concise function. In it, how would you add
        "AM/PM" to the times that are not midnight?

        Thanks, - Dave

        ZeldorBlat wrote:[color=blue]
        > Juliette wrote:[color=green]
        > > laredotornado@z ipmail.com wrote:[color=darkred]
        > > > Hi,
        > > >
        > > > I am retrieving results from an SQL query, one of which is a date
        > > >
        > > > $arrival = $row['itin_arrival_d ay'];
        > > >
        > > > I would like to write a PHP function that formats the date in the
        > > > following way:
        > > >
        > > > If the time part of the date is of the form 2006-05-19 00:00:00 in
        > > > which the time is midnight, I would like the result to be printed as
        > > > "05/19/06 ---" but if the time is anything other than midnight, for
        > > > example, "2006-05-21 18:00:00" I would like the printed result to be
        > > > "05/21/06 6:00 PM".
        > > >
        > > > How can I do this? - Dave
        > > >[/color]
        > >
        > >
        > > Untested and probably not the most efficient, but should work:
        > >
        > > $arrival_array = explode( ' ', $arrival );
        > >
        > > // format the date
        > > $date_array = explode( '-', $arrival_array[0] );
        > > $arrival_date = $date_array[1] . '/' . $date_array[2] . '/' . (
        > > $date_array[0] - 2000 );
        > > unset( $date_array );
        > >
        > > // format the time
        > > if( $arrival_array[1] === '00:00:00' ) {
        > > $arrival_time = '---';
        > > }
        > > else {
        > > $time_array = explode( ':', $arrival_array[1] );
        > > if( $time_array[0] > '12' ) {
        > > $arrival_time = ( $time_array[0] - 12 ) . ':' $time_array[1] . ' PM';
        > > }
        > > else {
        > > $arrival_time = $time_array[0] . ':' $time_array[1] . ' AM';
        > > }
        > > unset( $time_array );
        > > }
        > >
        > > print $arrival_date . ' ' . $arrival_time;[/color]
        >
        > Alternatively, try something like this:
        >
        > function foo($str) {
        > $t = strtotime($str) ;
        > return $t == mktime(0, 0, 0, date('m', $t), date('d', $t),
        > date('Y', $t)) ? date('m/d/y ---', $t) : date('m/d/y g:i', $t);
        > }[/color]

        Comment

        • ZeldorBlat

          #5
          Re: php function to format date's time


          laredotornado@z ipmail.com wrote:[color=blue]
          > Thanks for this neat, concise function. In it, how would you add
          > "AM/PM" to the times that are not midnight?[/color]

          Read this, it'll tell you everything you ever wanted to know about
          date() :

          <http://www.php.net/date>

          Comment

          Working...