Page 1 of 1

PHP and Date - Help me !!! :(

Posted: Mon Oct 06, 2008 5:49 am
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:

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

Posted: Mon Oct 06, 2008 5:59 am
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.

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

Posted: Mon Oct 06, 2008 6:05 am
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.....

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

Posted: Mon Oct 06, 2008 6:26 am
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.

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

Posted: Mon Oct 06, 2008 6:33 am
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....

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

Posted: Mon Oct 06, 2008 6:59 am
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";

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

Posted: Tue Oct 07, 2008 3:33 am
by ajju
Hi,

I need to skip sundays in this calculation,,,, how can this be done?

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

Posted: Tue Oct 07, 2008 3:37 am
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.

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

Posted: Tue Oct 07, 2008 12:14 pm
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'));
?>