Professional Web Applications Themes

Time problem - PHP Development

Hi All, Sorry if this has been asked a 1000 times and if its easy to find in the php manual but i cant seam to solve this. How do i convert a timestamp in to a normal readable time & date ie 20031111155023 into 11th November 2003 15:50:23 Many thanks, thought id ask someone is bound to have a snippet for this. Regards All Erin...

  1. #1

    Default Time problem

    Hi All,
    Sorry if this has been asked a 1000 times and if its easy to find in the
    php manual but i cant seam to solve this.

    How do i convert a timestamp in to a normal readable time & date ie

    20031111155023

    into

    11th November 2003 15:50:23


    Many thanks, thought id ask someone is bound to have a snippet for this.



    Regards

    All



    Erin
    Erin Guest

  2. #2

    Default Re: [PHP] Time problem

    --- Erin <co.uk> wrote: 

    That's not a timestamp, first of all. It looks to me like you just need to
    use substr() to p out the elements however you want. You can see if
    strtotime() can do anything with it, but I doubt it.

    If you want to convert it to a real timestamp, look at the mktime()
    function. Once you have a timestamp, it's very easy to work with, and you
    can use date() to format it however you want.

    Hope that helps.

    Chris

    =====
    My Blog
    http://shiflett.org/
    HTTP Developer's Handbook
    http://httphandbook.org/
    RAMP Training Courses
    http://www.nyphp.org/ramp
    Chris Guest

  3. #3

    Default Re: [PHP] Time problem

    Erin wrote:
     

    IFF the "timestamp" is coming from MySQL (which has that format), then
    you should use DATE_FORMAT() in your query to do the formatting.

    --
    ---John Holmes...

    Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

    php|architect: The Magazine for PHP Professionals www.phparch.com
    John Guest

  4. #4

    Default Re: Time problem

    Well,

    If your into your programming, have a look at this one :
    http://www.phpclasses.org/browse.html/package/1199.html

    It does something else, but the same approach can be used to
    format your readable dates. This wheel has surely been invented
    already, so you probably should do some googling to find a
    finnished class og function that does this for you.

    Kim

    "Erin" <co.uk> wrote in message
    news:pair.com... 
    the 
    Kim Guest

  5. #5

    Default Time problem

    I'm trying to use the current time, but I only see "13:1" as the hours and
    minutes. The seconds seem to update. Here is the code I'm using (before
    <html> tag):

    <?php
    if (isset($_COOKIE[lasthere]))
    {
    // process existing cookie
    $lasthere = $_COOKIE[lasthere];
    $user = $_COOKIE[user];

    // hour, minute, second, month, day, year
    $lastupdate = mktime(13, 0, 0, 5, 26, 2005);
    if ($lasthere < $lastupdate)
    {
    // update stuff
    $listnewstuff = "yes";
    }
    }
    else
    {
    // get user name
    }

    // in any case write a cookie out with the current date
    setcookie("lasthere", time(), time() + 36000);
    setcookie("user", "Jerry", time() + 36000);
    ?>

    The purpose is to see if you have been to the page since the last update.
    Your time here is always updated to the current time in the first setcookie.
    The page variable is set by the webmaster when he last updated the page. If
    you were here before the last update, I want to popup a page with new
    features. If you were here after the last update, then do nothing for the
    UI.

    Then in the body of the page, I'm putting:

    <?php
    echo "<br>lastupdate: " . date("M d, Y H:I:s", $lastupdate);
    echo '<br>now: ' . date("M d, Y H:I", time()) . "<br>";

    if ($listnewstuff = "yes")
    {
    echo "new stuff to be found at " . date("M d, Y H:I:s", $lasthere) .
    "<br>";
    }
    else
    {
    echo "no new stuff to be found at " . date("M d, Y H:I:s", $lasthere) .
    "<br>";
    }
    ?>

    Any ideas?
    TIA,
    Jer


    Lee Guest

  6. #6

    Default Re: Time problem

    Reformatted the code a little to fix a syntax error (if = = and not one).
    Here is what comes before the <html>:

    <?php
    $lasthere = $_COOKIE[lasthere];

    if (isset($lasthere))
    {
    // process existing cookie
    $user = $_COOKIE[user];

    // hour, minute, second, month, day, year
    $lastupdate = mktime(16, 16, 16, 3, 26, 2005);

    if ($lasthere < $lastupdate)
    {
    // updated stuff
    $listnewstuff = "yes";
    }
    else
    {
    // old stuff
    $listnewstuff = "no";
    }
    }
    else
    {
    // get user name
    $listnewstuff = "no cookie";
    }

    // in any case write a cookie out with the current date
    setcookie("lasthere", time(), time() + 36000);
    setcookie("user", "Kevin", time() + 36000);
    ?>

    and I check the varibles with this:

    <?php
    echo "<br>lastupdate: " . date("M d, Y H:I:s", $lastupdate);
    echo "<br>lasthere: " . date("M d, Y H:I:s", $lasthere);
    echo '<br>now: ' . date("M d, Y H:I", time());
    echo '<br>LNS: ' . $listnewstuff . '<br>';

    if ($listnewstuff == "yes")
    {
    echo "new stuff to be found<br>";
    }
    else
    {
    echo "no new stuff to be found<br>";
    }
    ?>

    The output is this:
    lastupdate: Mar 26, 2005 16:0:16
    lasthere: May 26, 2005 14:1:36
    now: May 26, 2005 14:1
    LNS: no
    no new stuff to be found

    I hit it a few seconds later and got this:
    lastupdate: Mar 26, 2005 16:0:16
    lasthere: May 26, 2005 14:1:15
    now: May 26, 2005 14:1
    LNS: no
    no new stuff to be found

    the seconds change, but the minutes is still "0".


    Lee Guest

  7. #7

    Default Re: Time problem

    "Lee David" <net> wrote in message
    news:com... 

    Yes, the manual IS hard to find, and even more difficult is to find the
    parameters of date() function, but after some furious three seconds I found
    them, and it seems your problem is that upper case I is not the same as
    lower case i.

    i Minutes with leading zeros
    I (capital i) Whether or not the date is in daylights savings time


    --
    Welcome to Usenet! Please leave tolerance, understanding
    and intelligence at the door. They aren't welcome here.
    eternal piste erection miuku gmail piste com


    Kimmo Guest

  8. #8

    Default Re: Time problem

    I was using Sams Teach Yourself PHP which on page 81 says "I" returns the minutes. It is wrong of course. That was one of the problems. Another possible problem was the non-quote of the cookie variable as indicated by PHP fast&easy web development (Premier Press page 246). I do try to research things before I ask questions. Once I found out the books were wrong, it fell into place. The internet would have been more accurate, but I can't read that on the train.

    Do you know how to open a new page and can it be modal? If it isn't modal, then I could use javascript to open a page or just popup a prompt box, but then the variables would be already set. And I would like to learn how to do it in PHP. What I'm after is if the cookie isn't set, then you are a new user and I'd like to get your name to personalize the pages. After the new page gets the info, the user variable would be set and the cookie updated. I couldn't find anything in the manual under "open" or "popup" that looked like a new page.

    Lee


    "Kimmo Laine" <com> wrote in message news:H1Ale.37$news.jippii.net... 
    >
    > Yes, the manual IS hard to find, and even more difficult is to find the
    > parameters of date() function, but after some furious three seconds I found
    > them, and it seems your problem is that upper case I is not the same as
    > lower case i.
    >
    > i Minutes with leading zeros
    > I (capital i) Whether or not the date is in daylights savings time
    >
    >
    > --
    > Welcome to Usenet! Please leave tolerance, understanding
    > and intelligence at the door. They aren't welcome here.
    > eternal piste erection miuku gmail piste com
    >
    >[/ref]
    Lee Guest

  9. #9

    Default Re: Time problem

    "Lee David" <net> wrote in message
    news:com... [/ref][/ref]

    Do you know how to open a new page and can it be modal? If it isn't modal,
    then I could use javascript to open a page or just popup a prompt box, but
    then the variables would be already set. And I would like to learn how to
    do it in PHP. What I'm after is if the cookie isn't set, then you are a new
    user and I'd like to get your name to personalize the pages. After the new
    page gets the info, the user variable would be set and the cookie updated.
    I couldn't find anything in the manual under "open" or "popup" that looked
    like a new page. >>>

    That sounds like a job for javascript. And no, it can't be modal. And no,
    you can't use it since headers are already sent when user gets the input.

    Instead try something like this: If no cookie is detected you redirect user
    to another page where they input their info.

    if(!$_COOKIE){
    header("location: http://www.your.domain.com/whatsyourname.php");
    exit(0);
    }

    After whatsyourname.php has gathered the cookie info, you can return to the
    page user was getting to.

    --
    Welcome to Usenet! Please leave tolerance, understanding
    and intelligence at the door. They aren't welcome here.
    eternal piste erection miuku gmail piste com


    Kimmo Guest

  10. #10

    Default Re: Time problem

    Thanks, that exactly what I'm looking for. The problem is if you have a
    different vocabulary you don't find the answer in a search engine and I
    hadn't hit it in either book yet.

    Lee


    Lee Guest

Similar Threads

  1. Time problem && Display manager problem
    By Andreas in forum Ubuntu
    Replies: 38
    Last Post: November 22nd, 04:06 PM
  2. Problem with xml loading a second time
    By ChuckyLeFrek in forum Macromedia Flash Data Integration
    Replies: 3
    Last Post: December 8th, 03:12 PM
  3. Time problem with Now()
    By wt in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 20th, 09:27 AM
  4. Run Time Problem
    By stefano mostarda in forum ASP.NET Building Controls
    Replies: 3
    Last Post: October 17th, 02:29 PM
  5. Formatting a time field to 24 hour time (Military time) in the Datagrid
    By David Hearn in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: September 10th, 02:50 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139