SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question split string of numbers php

    Hey If i have a string of numbers eg:

    Code:
    $date = 200703241230
    where it is: (year)(month)(day)(hour)(minute)
    how can i split the string so i can have it like:

    Code:
    $date_year = 2007
    $date_month = 03
    $date_day = 24
    $date_hour = 12
    $date_minute = 30
    so i can display it how i wish eg( 24/03/2007 12:30 )

    thanks in advance for your help.
    munnaz

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One way you can do it (if your strings are always going to be the same length), is to use substr() and just use different offsets.

    PHP Code:
    $date "200703241230";
    $date_year substr($date04);
    // And so on 

  3. #3
    SitePoint Zealot Bill Palmer's Avatar
    Join Date
    Oct 2005
    Location
    London, UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
            $date 
    200703241230;

            
    preg_match('/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/',$date,&$results);

            
    $year $results[1];
            
    $month $results[2];
            
    $day $results[3];
            
    $hour $results[4];
            
    $minute $results[5];

            echo 
    "$year $month $day $hour $minute";
    ?>
    Probably not the easiest regex, but it's 2am.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo date("d/m/Y H:i"strtotime($date)); 
    strtotime

  5. #5
    SitePoint Zealot Bill Palmer's Avatar
    Join Date
    Oct 2005
    Location
    London, UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    PHP Code:
    echo date("d/m/Y H:i"strtotime($date)); 
    strtotime
    That's a pretty cool function. I've never seen it before. I usually store all my dates as unix timestamps in the first place, so I've never needed it... but good to know!


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
  •