i need to get the first day from midnight
and last day until almost midnight
for a query i do where i want to check that the date is in the current month!
and the timme in the db is hold as unix_time!
thanks inadvane
peleg
first and last day
Moderator: General Moderators
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
Admin Edit: Added tags around code.[/color]
Lite...
Code: Select all
Code: Select all
<?PHP
// get current month and year
$month1 = date(“F”);
$year1 = date(“y”);
// get month and date of stored timestamp
$month2 = date(“F”, whatever_the_timestamp_value_is);
$year2 = date(“y”, whatever_the_timestamp_value_is);
// compare
$test1 = $month1.$year1;
$test2 = $month2.$year2;
if($test1 == $test2){
do this
} else {
do this instead.
}
?>Lite...
-
litebearer
- Forum Contributor
- Posts: 194
- Joined: Sat Mar 27, 2004 5:54 am
"i need to get the first day from midnight
and last day until almost midnight
for a query i do where i want to check that the date is in the current month!
and the timme in the db is hold as unix_time!
thanks inadvane
peleg"
from the above statement we can determine the following constants:
$start_day = 01
$start_hour = 12 AM
$start_minute = 00
$start_second = 01
If the current month = 1, 3, 5, 7, 8, 10 or 12 then
$end_day = 31
Else If current month = 4,6,9 or 11 then
$end_day = 30
Else If current year is a leap year then
$end_day = 29
Else
$end_day = 28
$end_hour = 11PM
$end_minute = 59
$end_second = 59
Using the above contants with the current month we can utilize the mktime() function (http://www.php.net/mktime) to create a beginning and ending timestamp.
We can then take the timestamp from your database cam compare to the begining and ending timestamps to determine if the database date is within the current month.
OR
We can do it the easy way and simply check the month and year values to reach the same conclusion. Unless one or more of the dates is being "twisted" by Einstein's Time Space Theory, equal month and year equate to being in the same period.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
$curr_time = time();
$curr_month = date('m',$curr_time);
$curr_year = date('Y',$curr_time);
$number_of_days_in_month = date('t',$curr_time);
$first_day_of_current_month = mktime(0,0,0,$curr_month,1,$curr_year);
$last_day_of_current_month = mktime(23,59,59,$curr_month,$number_of_days_in_month,$curr_year);
$curr_month = date('m',$curr_time);
$curr_year = date('Y',$curr_time);
$number_of_days_in_month = date('t',$curr_time);
$first_day_of_current_month = mktime(0,0,0,$curr_month,1,$curr_year);
$last_day_of_current_month = mktime(23,59,59,$curr_month,$number_of_days_in_month,$curr_year);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.