first and last day

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
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

first and last day

Post by pelegk2 »

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
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

Admin Edit: Added tags around code.[/color]

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...
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

what?this want what i asked for!
for example : now is april month
i want to know what the first time/date of the month , and what is the last!
beacuse i need to do a comae in a query :
$startMonth<=currentmonth<=$endofmonth
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

"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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

litebearer wrote: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.
lol

Mac
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

$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);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply