Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Date issues...

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Date issues...

Postby Scoodo » Thu Nov 15, 2012 1:17 pm

Hiya guys, I need a little bit of help.

I'm running a small script that takes a string from a database and then I'm trying to compare it to the server date, to see if it's in the future or past. I've managed this amount in my coding but I'm a bit stump on how to highlight if it's one week away etc.. E.g. I'm building a system for highlight stock being out of date, and I need to be able to give a weeks notice in the system, so I need to compare the date to the current server time and add like 7 days.. then will it be out of date.. etc.

Here's what I have, if anyone can help that'd be great.

Code: Select all
                               <td style="font-size:11px"><?php echo $row['Purchase_Date']; ?></td>
            <?php $sbd=$row['Sell_By_Date'];
            if (strtotime($sbd) < time())
            echo
            '<td style="font-size:11px; background-color:#FF6666; font-weight:bold">
            OUT OF DATE
            </td>';
            else if (strtotime($sbd) > time())
            echo
            '<td style="font-size:11px">'
            .$sbd.'
            </td>';
            ?>


Note: If you look closely, I've set the output as OUT OF DATE and changed the cell colour if the product is out of date.
Scoodo
New php-forum User
New php-forum User
 
Posts: 22
Joined: Tue Mar 20, 2012 12:09 pm

Re: Date issues...

Postby MeroD » Fri Nov 16, 2012 3:30 am

Try:
Code: Select all

$sbd 
date('Y-m-d'strtotime($row['Sell_By_Date']));
$notice strtotime('-7 day'$sbd);

if (( 
$notice time() ) && ( $sbd time() )) {
  
// About to expire...
}
elseif ( 
$sbd time() ) {
 
// Expired!               

else {
  
// Print SellbyDate...
}
 
MeroD
New php-forum User
New php-forum User
 
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: Date issues...

Postby Scoodo » Sat Nov 17, 2012 2:48 pm

Thanks MeroD, I can see where you're going with that answer, however I'm experiencing something that seems more difficult to overcome due to your answer. I get the following on the output page..

SCREAM: Error suppression ignored for
( ! ) Notice: A non well formed numeric value encountered in C:\wamp\www\ShopKontrols\Stock_Report2.php on line 54


Now I'm wondering if this is due to the Sell_By_Date being stored as a "Date" type in the database structure. (The error is requesting a numeric value). I'm guessing though. Any further help would be great.
Scoodo
New php-forum User
New php-forum User
 
Posts: 22
Joined: Tue Mar 20, 2012 12:09 pm

Re: Date issues...

Postby MeroD » Sat Nov 17, 2012 10:28 pm

You are correct. The old date() function and strtotime can be pretty stuck up at times, and are not considered good practice (according to stackoverflow anyway).

It is recommended to use either the new DateTime functions, or using MySQL's UNIX_TIMESTAMP() function.
SELECT UNIX_TIMESTAMP(timestamp) FROM sometable

The result will be a standard integer Unix timestamp, so you can do a direct comparison to time().
MeroD
New php-forum User
New php-forum User
 
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron