PHP and Date - Help me !!! :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ajju
Forum Newbie
Posts: 7
Joined: Sat Aug 16, 2008 1:34 am

PHP and Date - Help me !!! :(

Post by ajju »

Hi,

I am trying to achieve something with date, i hope at least this time i get some help :(

I am using three categories, contents in today date, yesterday date and tomorrow date.

I am saving date to variable to display in the web page.... pls check the following code

// To display today date
$today = mktime(0, 0, 0, date("m"), date("d"), date("y"));
echo( date("F d, Y", $today));

// To display tomorrow date
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
echo ( date("F d, Y", $tomorrow));

// To display yesterday date
$yesterday = mktime(0, 0, 0, date("m"), date("d")-1, date("y"));
echo ( date("F d, Y", $yesterday));

Are above codes correct to save date in a variable? according to above codes date can be displayed, but i need to display content of the table Where date=today and date=tomorrow and date=yesterday respectively, so i tried the following but in vein.... Please help me with this regards and i also need to skip Sunday in between........

The following code used to display the content for table...... is it rite?

$sql="SELECT * FROM $table WHERE date=$today ORDER BY time";
$sql="SELECT * FROM $table WHERE date=$tomorrow ORDER BY time";
$sql="SELECT * FROM $table WHERE date=$yesterday ORDER BY time";

Please help me with this problem....................... :cry:
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: PHP and Date - Help me !!! :(

Post by papa »

First of all I would save the dates in a more general date synthax, like: 2008-10-06 in case you want to change the format later on.
ajju
Forum Newbie
Posts: 7
Joined: Sat Aug 16, 2008 1:34 am

Re: PHP and Date - Help me !!! :(

Post by ajju »

Ok, I am very new to php, how to i really do it?

Let me make my query very simple, i need to display the content of the table only of a particular date.....
Last edited by onion2k on Mon Oct 06, 2008 6:16 am, edited 1 time in total.
Reason: Request for IM removed. Keep it to the thread so the solution is available for everyone.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: PHP and Date - Help me !!! :(

Post by papa »

Well, you can set up the mySQL table so the column you are added to has the "date" field type.

Then the the data you insert is: $yesterday = date("Y-m-d", $yesterday);

Just a quick answer to your question.
ajju
Forum Newbie
Posts: 7
Joined: Sat Aug 16, 2008 1:34 am

Re: PHP and Date - Help me !!! :(

Post by ajju »

Yes, I have the type date already in the table, its displaying perfectly, i only have to invoke the data (row) particular to the current date/ next/ previous date. i thought of WHERE clause. where i have put a code as provided above SELECT * ...... WHERE date= '--------' ORDER BY ..... ;

I tried different ways of filling '---------' as mentioned above, but not working....
ajju
Forum Newbie
Posts: 7
Joined: Sat Aug 16, 2008 1:34 am

Re: PHP and Date - Help me !!! :(

Post by ajju »

Hi papa,

Thanks a lot for your help...... I tried few things and now its working fine. the following is the code

Please let me know if there is any other simple way, :). Coders never rest in peace... :)

----------Current Day------
<?php
$today = mktime(0, 0, 0, date("m"), date("d"), date("y"));
echo( date("F d, Y", $today));
$today1 = date("Y-m-d", $today);
?>

$sql="SELECT * FROM $intrvdtls WHERE date='$today1' ORDER BY time";

------------Next Day--------------
<?php
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
echo ( date("F d, Y", $tomorrow));
$tomorrow1 = date("Y-m-d", $tomorrow);

$sql="SELECT * FROM $intrvdtls WHERE date='$tomorrow1' ORDER BY time";

-------------Previous Day-----------
<?php
$yesterday = mktime(0, 0, 0, date("m"), date("d")-1, date("y"));
echo ( date("F d, Y", $yesterday));
$yesterday1 = date("Y-m-d",$yesterday);
?>

$sql="SELECT * FROM $intrvdtls WHERE date='$yesterday1' ORDER BY time";
ajju
Forum Newbie
Posts: 7
Joined: Sat Aug 16, 2008 1:34 am

Re: PHP and Date - Help me !!! :(

Post by ajju »

Hi,

I need to skip sundays in this calculation,,,, how can this be done?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP and Date - Help me !!! :(

Post by onion2k »

date("w") will tell you the day of the week. Check if it's Sunday and if it is generate a new date for the next day.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP and Date - Help me !!! :(

Post by RobertGonzalez »

You can always use strtotime() to get your date values:

Code: Select all

<?php
$format = 'Y-m-d';
$today = date($format);
$tomorrow = date($format, strtotime('tomorrow'));
$yesterday = date($format, strtotime('yesterday'));
?>
Post Reply