Get current date and time form mktime in PHP - php

I have a problem with find the current date from past mktime. In PHP I find the current date using date("j");. Here I need, suppose my date was in the past year like mktime(0, 0, 0, 2, 1, 2008), then here how can I find the current date of this particular past month.

Either as #octern's solution, or you can do
$day = date('j', strtotime("-2 months"));
or
$day = date('j', strtotime('-30 days'));
depending on your need.
You may also want to refer to strtotime() manual.

Try this:
$date = getdate(mktime(0, 0, 0, 2, 1, 2008));
$day = $date['mday'];
Or just:
$day = date('j', mktime(0, 0, 0, 2, 1, 2008))

Related

How many seconds until my PHP mktime future date

So I have my code that fetches the date of the first of the next month at midnight:
$future_date = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m")+1, 1, date("Y")));
What I can't figure out (and I've Googled a fair bit) is how to count the seconds from NOW until that future date.
Storing mktime() into $future_date_unix means you can use it for both the string generation you require and the seconds calculation using a subtraction of the current time() value.
$future_date_unix = mktime( 0, 0, 0, date('m') + 1, 1, date('Y') );
$future_date = date( 'Y-m-d H:i:s', $future_date_unix );
$seconds_till_future_date = $future_date_unix - time();

Can't get last day of June with PHP

Comment is the output I'm seeing. Help!
echo date("Y-m-t", strtotime("2012-07-31 -1 month")); //2012-07-31
echo date("Y-m-t", strtotime("2012-07-31 -2 month")); //2012-05-31
July has 31 days, so it can't be processing it as August 1, right? Even if it were, the second line should work. Right?!
Thanks!
What's happening here is, "7-31" minus one month comes out as "6-31", which translates to "7-1", which - asking for the last day of the month - comes out as "7-31".
When you want to do month math, it's almost always, if not always, better to build the time using mktime.
$month = 7;
echo date("Y-m-t", mktime(0, 0, 0, $month - 1, 1, 2012)); //2012-06-30
Note that when doing month math in mktime, always give '1' as the day. Or really anything as long as it's 28 or lower.
You can do something like this:
$month = date('m') ;
$day = date('d') ;
$year = date('Y') ;
// ----
$current = mktime( 0, 0, 0, $month , $day, $year);
$yesterday = mktime( 0, 0, 0, $month , $day-1, $year);
$tomorrow = mktime( 0, 0, 0, $month , $day+1, $year);
// ----
echo '<pre>Current Day: '.date('Y-m-d', $current).'</pre>';
echo '<pre>Day Before: '.date('Y-m-d', $yesterday).'</pre>';
echo '<pre>Day After: '.date('Y-m-d', $tomorrow).'</pre>';
If you're looking for the last day of July:
$july = mktime( 0, 0, 0, 8, 1-1, 2012);
// Last Day of July: 2012-07-31
echo '<pre>Last Day of July: '.date('Y-m-d', $july).'</pre>';
Using mktime, just enter the first day of August and subtract one day.

How to set an specific date?

I'm trying to create an date with
$date_end = mktime(0, 0, 0, date('m'), date('d')+7, date('Y'), $date_set);
The output is today + 7 days instead of the date given + 7.
The manual says nothing about mktime() taking a date as argument.
Use strtotime("+7 days", $date_set).
$date_end = mktime(0, 0, 0, date('m', $date_set), date('d', $date_set)+7, date('Y', $date_set));
is, I believe, what you were trying to accomplish (assuming $date_set is a timestamp). Else, #Kristian's suggestion I believe is a good one.
Why are you passing a $date_set variable, and why are you using mktime if you already have the time?
Simply add 7 days: $date_end = $date_set + (7 * 86400);

First date of a week

I want a First Day Of a week say I have 45th week. Now I want to have date of the first sunday of this week.
Can any one suggest me how I can go about this ?
Thank you
Searching around a bit, the most suggested is:
$year = "2009"; // date("Y");
$week = "45"; // date("W");
$firstDayOfWeek = strtotime($year."W".str_pad($week,2,"0",STR_PAD_LEFT));
print("The first day of week ".$week." of ".$year." is ".date("D, d-m-Y",$firstDayOfWeek));
Basically this comes down to letting strtotime do the work for you. You fill in "2009W45" and it'll retrieve the date for you.
Pitfall here is that the week needs to be in a 2 digit format. So week 1 needs to be 01, hence the str_pad to zero-fill it.
you should strtotime and date function
some code like that
$next_sunday = strtotime('next sunday');
$next_week = strtotime('+1 week');
$next_sunday_of_next_week = strtotime('next sunday', $next_week);
hope this helps
If you have the month, day and year:
$timestamp = mktime(0, 0, 0, $month, $day, $year);
echo date('c', $timestamp) = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year);
you could also do it this way if you have the timestamp:
echo $date('c', mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
From: http://pinoytech.org/blog/post/get-the-first-day-of-the-week-with-an-exact-date
date($str_format, strtotime($year."W".$week."1"))
Where $str_format is the output formate according to the PHP date() function.. I use 'M d Y'

php date functions

I've rarely touched PHP date functions,
and now I need to do the follows:
get current date,
get date of three days later
get date of three weeks later
get date of three month later
get date of three years later
and finally to implement such a function:
function dage_generate($number,$unit)
{
}
$unit can be day/week/month/years
http://uk.php.net/strtotime can do most of that:
strtotime("today")
strtotime("+ 3 days")
strtotime("+ 3 weeks")
strtotime("+ 3 months")
strtotime("+ 3 years")
The function would be something like:
function dage_generate($number,$unit)
{
return strtotime("+ ".$number." ".$unit);
}
http://us.php.net/manual/en/function.date.php
Note towards the bottom of the page:
Example #3 date() and mktime() example
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
Using strtotime() you can do this easily.
$now = time();
$threeDays = strtotime("+3 days");
$threeWeeks = strtotime("+3 weeks");
$threeMonths = strtotime("+3 months");
$threeYears = strtotime("+3 years");
Each of those variables will be an integer which represents the unix timestamp at that point in time. You can then format it into a human readable string using date().
echo date('r', $threeWeeks);
// etc...
Use date_create() to create a DateTime object, then use the add method.
See the examples on the page for the add() method, they contain all you need.
PHP Date/Time Functions with examples

Categories