SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 36
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    timestamp not showing correctly

    Hi All,

    I moved my server and SQL database and now getting errors showing timestamp

    old server the time stamp

    format was 20050316134117

    with `time_stamp` timestamp(14) NOT NULL

    and the new server has 2006-04-21 15:27:11

    with `time_stamp` timestamp NULL default CURRENT_TIMESTAMP,

    the script I am using is

    Code:
            $time_stamp = stripslashes($cats[time_stamp]);
            $year = substr($time_stamp, 0, 4);
            $month = substr($time_stamp, 4, 2);
            $day = substr($time_stamp, 6, 2);
            $hour = substr($time_stamp, 8, 2);
            $min = substr($time_stamp, 10, 2);
            $t = mktime($hour, $min, 0, $month, $day, $year);
    
            $date = date('M d\t\h\, Y \a\t h\:i a', $t);
    instead of showing the result as

    April 21st, 2006 at 03:27 pm

    all the results show

    Dec 04th, 2005 at 09:01 pm

    Please suggest what changes I should make in the script

  2. #2
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is why I always use UNIX time. No problems with migrating servers or anything and way easier to handle .

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    MySQL has a UNIX_TIMESTAMP() function you can use to retrieve that from a datetime/timestamp column. You can also pass the new timestamp string you're getting to strtotime() in PHP to get a timestamp from it without having to try to split up the string yourself.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please,

    I would rate my Knowledge rating in scale 1 to 10 as 2.

    Please......... help me the code... I am still learning.

    regards

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please any help

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    $date date('M d\t\h\, Y \a\t h\:i a'strtotime($cats['time_stamp'])); 

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Dan,

    Thanks a lot for the code.... I am short of words for thanks.

    The only problem I am seeing is that the time shows off by 54 minutes... means the data I potsed at time 10:56 pm but the data posted shows 10:02 pm

    Any suggestions.

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    Thanks for the cluse I got it resolved by adding + 7200 secs to the cur time. 2 hrs is the time difference.

    Thanks for the code and help to get this resolved.

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Dan,

    I am trying to explore further and learn on the time stamp

    What I am trying to learn

    1. Picks from Database expire time
    2. Declares expire date
    3. Compare to current time
    4. Declares days left
    5. Displays image expire if day passed, ending < 1 day, new >1 day
    6. Add days if more than 1 day or display day if <= 1 day

    Here what I have done

    Code:
            $time_exp = strtotime($cats['item_exp']);//expire date from Database
            $dateexpire = $time_exp + 7200;//add server timedifference
            $date_expire = date('m\/d\/Y', $dateexpire);//display date
    
            $curDate=mktime() + 7200; //current UNIX timestamp.
            $diff= $dateexpire - $curDate ; //date diff in seconds
            $diff = round ($diff / 86400); //date diff in days
    
             if ($dateexpire > $curDate)
              $img = "<img src='/images/new.gif' border='0'>";//display new image if expire date > 1 day.
             else if ($dateexpire < $curDate)
              $img = "<img src='/images/exp.gif' border='0'>";//display expire image if expire date passed.
             else if ($dateexpire = $curDate)
              $img = "<img src='/images/ending.gif' border='0'>";//display ending image if expire date = 1 day.
    
    echo "$date_expire ($diff)$img";
    The problem I am having is that the difference from current date to today's expire date shows -1 and not 0.

    Are the if statements correct and also what if statement I should use to display days ("s" gets added for more than 1 days) and it displays day if only 1 day is left.

    Please suggest.

    TIA.

  10. #10
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can retrieve the number of days between the current date and the expiration date directly from the database:

    SELECT (TO_DAYS(NOW()) - TO_DAYS(date_expire)) AS daysToExpiration FROM table;

    PHP Code:
    if ($daysToExpiration == || $daysToExpiration == -1) {
        echo 
    $daysToExpiration " day";
    } else {
        echo 
    $daysToExpiration " days";

    I'm assuming you're using mySQL since you didn't say anything to the contrary when I mentioned it earlier.

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Dan,

    I am not able to follow your suggestion

    I am using MYSQL and the code I am using is

    Code:
    $display_block = "";
    
    //show categories first
    $get_cats =  "SELECT * FROM store_item WHERE item_exp LIKE '200%%%%%' ORDER BY `item_exp`ASC  limit $dept_id,$limit";
    $get_cats_res = mysql_query($get_cats) or die(mysql_error());
    
    if (mysql_num_rows($get_cats_res) < 1) {
       $display_block = "<P><em>Sorry, no categories to browse.</em></p>";
    } else {
       while ($cats = mysql_fetch_array($get_cats_res)) {
            $item_id  = $cats[item_id];
            $sort_id  = $cats[sort_id];
            $item_title = stripslashes($cats[item_title]);
            $item_desc = stripslashes($cats[item_desc]);
            $item_code = stripslashes($cats[item_code]);
            $item_exp = stripslashes($cats[item_exp]);
            $item_image = stripslashes($cats[item_image]);
            $store_image = stripslashes($cats[store_image]);
            $item_subdesc = stripslashes($cats[item_subdesc]);
    
    $display_block .= "<p>$item_title<br>$item_subdesc</p>;
    In the display block is where I want to add $date_expire ($diff)$img

    Please suggest.

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi All,

    Any suggestions or help?

    regards

  13. #13
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $display_block "";

    //show categories first
    $get_cats =  "SELECT 
                    item_id,
                    sort_id,
                    item_title,
                    item_desc
                    item_code,
                    item_exp,
                    item_image,
                    store_image,
                    item_subdesc,
                    TO_DAYS(item_exp) - TO_DAYS(CURDATE()) AS item_days_to_expiration
                  FROM store_item
                  WHERE item_exp LIKE '200%%%%%' 
                  ORDER BY `item_exp` ASC
                  LIMIT 
    $dept_id,$limit";
              
    $get_cats_res mysql_query($get_cats) or die(mysql_error());

    if (
    mysql_num_rows($get_cats_res) < 1) {
       
    $display_block "<P><em>Sorry, no categories to browse.</em></p>";
    } else {
       while (
    $cats mysql_fetch_array($get_cats_res)) {
            
    $item_id  $cats['item_id'];
            
    $sort_id  $cats['sort_id'];
            
    $item_title stripslashes($cats['item_title']);
            
    $item_desc stripslashes($cats['item_desc']);
            
    $item_code stripslashes($cats['item_code']);
            
    $item_exp stripslashes($cats['item_exp']);
            
    $item_image stripslashes($cats['item_image']);
            
    $store_image stripslashes($cats['store_image']);
            
    $item_subdesc stripslashes($cats['item_subdesc']);
            
    $item_days_to_expiration $cats['item_days_to_expiration'];

            
    $display_block .= "<p>$item_title<br>$item_subdesc</p>";

    Last edited by Dan Grossman; Apr 26, 2006 at 06:08.

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    Thanks for the code.... it worked

    I am adding you other suggestion

    Code:
    if ($item_days_to_expiration == 1 || $item_days_to_expiration == -1) {
        echo $item_days_to_expiration . " day";
    } else {
        echo $item_days_to_expiration . " days";
    }
    1. What I get is for "0" (means expiring today) as "0 days"
    2. for "1" (means expiring tomorrow) as "1 day"
    3. for "2" as "2days"

    what should I change in the if statement to show

    1. for day passed shows "expired"
    2. for "0" as "0 day&nbsp;HOT"
    3. for "1" as "1 day&nbsp;ENDING"
    4. for ">1" as "2 days&nbsp;NEW"

    Please suggest

  15. #15
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Surely, having already written the code you provided in your original post, you can figure out how to make wording changes to simple echo statements. Please try, as doing it yourself is the best way to learn, and there's nothing new being demonstrated if I do that for you.

  16. #16
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($item_days_to_expiration 0) {
        
    $word " expired";
    }
    elseif (
    $item_days_to_expiration == 1) {
        
    $word " day";
    } else {
        
    $word " days";

    Edit:

    I agree with Dan, but I'd already started typing
    Ian Anderson
    www.siteguru.co.uk

  17. #17
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan & Siteguru,

    I totally agree to your point and working little bit was able to get it to work.

    I need a small guidance.

    In item_exp, sometimes the filed is blank as the item has to exp date

    and I want to show as it as "Unknown"

    Here is what I am trying

    Code:
    if ($date_expire == "") {
        $datetoexpire = "<font face='Verdana, Arial, Helvetica' size='1' color='red'>Unknown</font>";
    } else {
        $datetoexpire = " $date_expire . ";
    }
    I am not getting it to show Unknown.... rather it leaves a blank space.

    Any suggestions.

  18. #18
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Which means $date_expire is never an empty string, it actually has data of some other type (e.g. " " rather than ""). Unless you identify exactly which information type is actually in the database then we'll be chasing out tails forever.
    Ian Anderson
    www.siteguru.co.uk

  19. #19
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    siteguru,

    The data filed is left empty file entering the data.... I am not sure what to tell you. If I access the data the filed says "Null"

    How to find out?

  20. #20
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    A NULL value is not an empty string. I take it you do know where and how to find the PHP manual? (Look at the is_null() function).

    I think it's time for you to start putting on your thinking cap and doing some research of your own - we are happy to help, but we aren't going to hold your hand through every minor step, nor write your code for you).
    Ian Anderson
    www.siteguru.co.uk

  21. #21
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi siteguru,

    Thanks for the beating .... I got it to work by using if (empty())

    Thanks for the guidance.

    regards

  22. #22
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to delete records from Database if day passed

    Hi Dan & Siteguru,

    Further going on the learning what now I am trying to learn is that compare the row 'item_exp' to the current time, if the time has passed, the record needs to be remove from the database.

    I will use cronjob to run it.

    Code:
    $SQL = "Delete from $store_item where item_exp < UNIX_TIMESTAMP (date_sub(now() interval 1 day))";
    Please suggest.

    TIA.

  23. #23
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    DELETE FROM $store_item WHERE TO_DAYS(item_exp) - TO_DAYS(NOW()) < 0;

    Test before you mess with important data.

  24. #24
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    I ran the query

    Code:
    $sql = mysql_query("DELETE FROM $store_item1 WHERE TO_DAYS(item_exp) - TO_DAYS(NOW()) < 0");
    but it did not delete the data

  25. #25
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to end the string with a double quote.


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
  •