GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Nov-2006, 03:48
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

date of Yesterday


I want to create the date of yesterday for date entered. I got the date for yesterday as:

PHP Code:

$yesterday  = mktime (date ("H"), date ("i"), date ("s"), date("m"), date "d") -1, date("Y"));
    $bhari = date ("d", $yesterday);
    $bln = date ("m", $yesterday);
    $tahun = date ("Y", $yesterday); 



but, if I enter day=6, month=10 year-2006, how can I get yesterday's date for above date? And how can I get the date for day before $yesterday?
Last edited by LuciWiz : 28-Nov-2006 at 06:23. Reason: Please insert your Php code between [php] & [/php] tags
  #2  
Old 28-Nov-2006, 07:26
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: date of Yesterday


You can easily get the date of yesterday by using strtotime().

Here are a couple of examples:

PHP Code:

<?php
// yesterday in 'd-m-Y' format
$uts['yesterday'] = strtotime( '-1 days' ); // or, strtotime( 'yesterday' );
echo "Yesterday: ", date( 'd-m-Y', $uts['yesterday'] ), "<br />\n";

// day-before-yesterday in 'd-m-Y' format
$uts['day-before-yesterday'] = strtotime( '-2 days' );
echo "The day before yesterday: ", date( 'd-m-Y', $uts['day-before-yesterday'] ), "<br />\n";
?>


You can refer to http://www.gnu.org/software/tar/manu...19.html#SEC119 for examples of valid (relative) date strings that you can use with strtotime().
  #3  
Old 28-Nov-2006, 21:32
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: date of Yesterday


It uses the system date, but if I enter my own date for example: day=6, month = 10, year = 2006, how can I get the date for day before above date.
  #4  
Old 29-Nov-2006, 01:57
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: date of Yesterday


If you look at the details of the strtotime function, you will see that it expects an optional timestamp value too.

int strtotime ( string time [, int now] )

So, if you omit passing a timestamp in the second parameter, you will be expecting the results relative to the system date, as per my examples above.

If you have your own timestamp, you can submit it to this second parameter and the calculations will be relative to this value, and not the system date.

Here is a new example:

PHP Code:

<?php
$mydate = '2006-10-06'; // ISO 8601 format.
                        // see http://www.gnu.org/software/tar/manual/html_node/tar_115.html#SEC115
                        // for other valid date/calendar formats.

// mydate into unixtimestamp
$uts['mydate'] = strtotime( $mydate );

// mydate - 1 (days)
$uts['mydate-1d'] = strtotime( '-1 day', $uts['mydate'] );

// mydate - 2 (days)
$uts['mydate-2d'] = strtotime( '-2 days', $uts['mydate'] );

// mydate - 1 (months)
$uts['mydate-1m'] = strtotime( '-1 months', $uts['mydate'] );

// display results.
echo "mydate: ",                date( 'd-m-Y', $uts['mydate'] ),    "<br />\n";
echo "mydate minus 1 day: ",    date( 'd-m-Y', $uts['mydate-1d'] ),    "<br />\n";
echo "mydate minus 2 days: ",    date( 'd-m-Y', $uts['mydate-2d'] ),    "<br />\n";
echo "mydate minus 1 month: ",    date( 'd-m-Y', $uts['mydate-1m'] ),    "<br />\n";
?>

  #5  
Old 29-Nov-2006, 03:10
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Re: date of Yesterday


Yes, of course.

$mydate = '2006-10-01' is just a string.

It could have been written like this as well: $mydate = "$year-$month-$day"; where $year is 2006, $month is 10 and $day is 6.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #6  
Old 29-Nov-2006, 03:29
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: date of Yesterday


Thank you so much, I've got the solution.
  #7  
Old 29-Nov-2006, 03:37
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Re: Get yesterday's date


One last thing, reading the page again, I found this important detail.

Quote:
For numeric months, the ISO 8601 format `year-month-day' is allowed, where year is any positive number, month is a number between 01 and 12, and day is a number between 01 and 31. A leading zero must be present if a number is less than ten. If year is 68 or smaller, then 2000 is added to it; otherwise, if year is less than 100, then 1900 is added to it. The construct `month/day/year', popular in the United States, is accepted. Also `month/day', omitting the year.

Emphasis mine.

So, '2006-10-01' is correct and '2006-10-1' is not!?
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Overloaded Constructors and Class within Class jdbrine C++ Forum 2 10-Jul-2006 23:14
Current Date (Short Date) In C Vishy80 C Programming Language 2 07-Jun-2005 14:54
Limit combo box and date time picker choice according to database created in folder shinyhui C++ Forum 0 22-Feb-2005 21:16
Limit combo box and date time picker choice according to database created in folder shinyhui MS Visual C++ / MFC Forum 0 22-Feb-2005 03:13
Rookie problem with date format cave monkey MySQL / PHP Forum 4 18-Mar-2004 09:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.