Variable substitution to replace switch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • damezumari

    Variable substitution to replace switch


    >From this page I made this function to add time to a datetime
    variable:

    function addtime ($datetime, $add, $type)
    {
    // How to add time to $datetime (data type datetime) and return it as
    datetime on the format 'Y-m-d H:i:s'
    // example calls:
    // $datetime = addtime ($datetime, 4, 'hours') // add 4 hours
    // $datetime = addtime ($datetime, 2, 'months') // add 2 months
    // $datetime = addtime ($datetime, 7, 'days') // add 1 week
    // tested it with: die($now.'<br />'.addtime($now , 4, 'months'));
    // anomaly: adding 4 months to 2007-05-31 09:50:52 gave 2007-10-01
    09:50:52. I would have preferred 2007-09-31 09:50:52
    $timestamp = strtotime($date time);
    $date_time_arra y = getdate($timest amp);
    $hours = $date_time_arra y['hours'];
    $minutes = $date_time_arra y['minutes'];
    $seconds = $date_time_arra y['seconds'];
    $months = $date_time_arra y['mon'];
    $days = $date_time_arra y['mday'];
    $years = $date_time_arra y['year'];

    switch ($type)
    {
    case 'hours':
    $hours = $add + $hours;
    break;
    case 'minutes':
    $minutes = $add + $minutes;
    break;
    case 'seconds':
    $seconds = $add + $seconds;
    break;
    case 'months':
    $months = $add + $months;
    break;
    case 'days':
    $days = $add + $days;
    break;
    case 'years':
    $years = $add + $years;
    break;
    }
    $timestamp = mktime($hours, $minutes, $seconds, $months, $days,
    $years);
    $newdatetime = date('Y-m-d H:i:s', $timestamp);
    return ($newdatetime);
    }

    It works OK, but I would like to replace the switch statement with one
    statement a la:

    &$type = &$type + $add;

    Can that be done in php?

    Regards,

    Jan Nordgreen

  • Rami Elomaa

    #2
    Re: Variable substitution to replace switch

    damezumari kirjoitti:

    >
    >>From this page I made this function to add time to a datetime
    variable:
    >
    function addtime ($datetime, $add, $type)
    {
    // How to add time to $datetime (data type datetime) and return it as
    datetime on the format 'Y-m-d H:i:s'
    // example calls:
    // $datetime = addtime ($datetime, 4, 'hours') // add 4 hours
    // $datetime = addtime ($datetime, 2, 'months') // add 2 months
    // $datetime = addtime ($datetime, 7, 'days') // add 1 week
    // tested it with: die($now.'<br />'.addtime($now , 4, 'months'));
    // anomaly: adding 4 months to 2007-05-31 09:50:52 gave 2007-10-01
    09:50:52. I would have preferred 2007-09-31 09:50:52
    $timestamp = strtotime($date time);
    $date_time_arra y = getdate($timest amp);
    $hours = $date_time_arra y['hours'];
    $minutes = $date_time_arra y['minutes'];
    $seconds = $date_time_arra y['seconds'];
    $months = $date_time_arra y['mon'];
    $days = $date_time_arra y['mday'];
    $years = $date_time_arra y['year'];
    >
    switch ($type)
    {
    case 'hours':
    $hours = $add + $hours;
    break;
    case 'minutes':
    $minutes = $add + $minutes;
    break;
    case 'seconds':
    $seconds = $add + $seconds;
    break;
    case 'months':
    $months = $add + $months;
    break;
    case 'days':
    $days = $add + $days;
    break;
    case 'years':
    $years = $add + $years;
    break;
    }
    $timestamp = mktime($hours, $minutes, $seconds, $months, $days,
    $years);
    $newdatetime = date('Y-m-d H:i:s', $timestamp);
    return ($newdatetime);
    }
    >
    It works OK, but I would like to replace the switch statement with one
    statement a la:
    >
    &$type = &$type + $add;
    >
    Can that be done in php?
    I'm not sure if I understood, but let me introduce a cool function for
    you: strtotime(), see http://php.net/strtotime

    Basicly you can do this:
    echo date('Y-m-d H:i:s', strtotime('2007-05-31 16:12:00 +2hours'));

    Would this be useful to you?

    --
    Rami.Elomaa@gma il.com

    "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
    usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: Variable substitution to replace switch

      At Thu, 31 May 2007 06:02:30 -0700, damezumari let h(is|er) monkeys type:
      [snip]
      >
      switch ($type)
      {
      case 'hours':
      $hours = $add + $hours;
      break;
      case 'minutes':
      $minutes = $add + $minutes;
      break;
      case 'seconds':
      $seconds = $add + $seconds;
      break;
      case 'months':
      $months = $add + $months;
      break;
      case 'days':
      $days = $add + $days;
      break;
      case 'years':
      $years = $add + $years;
      break;
      }
      $timestamp = mktime($hours, $minutes, $seconds, $months, $days,
      $years);
      $newdatetime = date('Y-m-d H:i:s', $timestamp);
      return ($newdatetime);
      }
      >
      It works OK, but I would like to replace the switch statement with one
      statement a la:
      >
      &$type = &$type + $add;
      >
      Can that be done in php?
      >
      $$type += $add;

      But, why are you reinventing the strtotime function?
      $dayaftertomorr ow = date('D M j G:i:s T Y',strtotime('+ 2 days'));

      HTH,

      Sh.

      --
      Schraalhans Keukenmeester - schraalhans@the .spamtrapexampl e.nl
      [Remove the lowercase part of Spamtrap to send me a message]

      "strcmp('apples ','oranges') < 0"

      Comment

      • damezumari

        #4
        Re: Variable substitution to replace switch

        Thanks!

        strtotime()= it is!

        It would still be good to get my question answered:

        "It works OK, but I would like to replace the switch statement with
        one
        statement a la:

        &$type = &$type + $add;

        Can that be done in php?"

        Regards,

        Jan Nordgreen

        Comment

        • Rami Elomaa

          #5
          Re: Variable substitution to replace switch

          damezumari kirjoitti:
          Thanks!
          >
          strtotime()= it is!
          >
          It would still be good to get my question answered:
          >
          "It works OK, but I would like to replace the switch statement with
          one
          statement a la:
          >
          &$type = &$type + $add;
          >
          Can that be done in php?"
          If they are unix timestamps and seconds yes. If not, you need to convert
          them into such first. This is what strtotime does, and then using date
          again reverses it.

          You can do this:
          $time0 = time(); // get a unix timestamp
          $interval = 60*60*24; // one day in seconds.
          $time0 += $interval; // add an interval to timestamp
          echo date('Y-m-d H:i:s', $time0); // tomorrow

          But this is good just for timestamps. Just remeber, working with
          timestamps is dreadful when it comes to daylight saving, leap years and
          other temporal oddities.

          --
          Rami.Elomaa@gma il.com

          "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
          usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

          Comment

          • Rami Elomaa

            #6
            Re: Variable substitution to replace switch

            Rami Elomaa kirjoitti:
            damezumari kirjoitti:
            >Thanks!
            >>
            >strtotime()= it is!
            >>
            >It would still be good to get my question answered:
            >>
            >"It works OK, but I would like to replace the switch statement with
            >one
            >statement a la:
            >>
            >&$type = &$type + $add;
            >>
            >Can that be done in php?"
            >
            Okay sorry, forget my reply, I've been reading your question wrong, I
            didn't get what you actually were doing. Sorry, please ignore me. :)

            --
            Rami.Elomaa@gma il.com

            "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
            usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

            Comment

            • Schraalhans Keukenmeester

              #7
              Re: Variable substitution to replace switch

              At Thu, 31 May 2007 08:38:40 -0700, damezumari let h(is|er) monkeys type:
              Thanks!
              >
              strtotime()= it is!
              >
              >
              &$type = &$type + $add;
              >
              Can that be done in php?"
              >
              You missed that bit in my first reply I think...

              $$type += $add;

              Read about variable variables here:


              Rgds,
              Sh.

              --
              Schraalhans Keukenmeester - schraalhans@the .Spamtrapexampl e.nl
              [Remove the lowercase part of Spamtrap to send me a message]

              "strcmp('apples ','oranges') < 0"

              Comment

              • damezumari

                #8
                Re: Variable substitution to replace switch

                Yes, you are right. I did miss it.

                Thanks for the solution and the link.

                Regards,

                Jan Nordgreen

                Comment

                Working...