Getting the Date and numeric weekday in PHP - php

i'm developing an application in PHP and I need to use dates and the numeric representation of weekdays.
I've tried the following:
$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', strtotime($tomorrow));
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";
Output
Today: 2016-11-11 weekday: 5
Tomorrow: 2016-11-12 weekday: 4
This isn't right because the weekday of tomorrow should be 6 instead of 4.
can someone help me out?

Using DateTime would provide a simple solution
<?php
$date = new DateTime();
echo 'Today: '.$date->format( 'Y-m-d' ) .' weekday '. $date->format( 'N' )."\n";
$date->modify( '+1 days' );
echo 'Tomorrow: '.$date->format( 'Y-m-d' ) .' weekday '. $date->format( 'N' )."\n";
Output
Today: 2016-11-11 weekday 5
Tomorrow: 2016-11-12 weekday 6
However the day numbers are slightly different, the N respresents the weekday number and as you can see Friday (Today) is shown as 5. With that Monday would be 1 and Sunday would be 7.
If you look at the example below you should get the same result
echo date( 'N' );
Output
5
Date Formatting - http://php.net/manual/en/function.date.php

You have little error in the code, here`s the working one:
$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', $tomorrow);
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";

DateTime is the Object Oriented method of working with dates in PHP. I find it to be working much more fluently. That aside, it looks alot better.
// Create a new instance
$now = new DateTime();
echo $now->format('N');
// Next day
$now->modify('+1 day');
echo $now->format('N');
Resources
DateTime manual - PHP.net

You almost have it right but not quite. Why are you using strtotime on $number2? Change it to $number2 = date('N', $tomorrow); and it will work.

Related

How to get next month on same day in php

How to get the next month on same day in php.
By using this code. I am able to get next month
$delidate = '2014-01-13'; // Today is Monday
$monthlyDate = strtotime("+1 month".$delidate);
$monthly = date("Y-m-d", $monthlyDate);
echo $monthly;
Output of this code is:- 2014-02-13
Now, I want to get the next month date on Monday only.
I need next month monday day 2014-02-10
or Suppose if day is Wednesday then i need Wednesday date on next month.
Updated:- Suppose if Particular day is not available on next date in that case i need nearest day in next month.
Update:-
I try this code
$delidate = '2014-01-29';
$monthlyDate = strtotime( $delidate . " +1 month " );
$monday = strtotime( date("Y-m-d", $monthlyDate) . " Monday this week " );
$monthly = date("Y-m-d", $monday);
echo $monthly;
It give me output 2014-03-03
while i need 2014-02-26
Try this
$delidate = '2014-01-13';
$monthlyDate = strtotime( $delidate . " +1 month " );
$monday = strtotime( date("Y-m-d", $monthlyDate) . " Monday this week " );
$monthly = date("Y-m-d", $monday);
echo $monthly;
You can do like this, since you want get same date after 1 Month
$delidate = '2014-01-13'; // Today is Monday
$monthlyDate = strtotime("+4 week".$delidate);
$monthly = date("Y-m-d", $monthlyDate);
echo $monthly;
Try this. This will take into consideration the current day not only Monday.
$delidate = '2014-01-13';
$monthlyDate = strtotime( $delidate . " +1 month " );
$dayname = strtotime( date("Y-m-d", $monthlyDate) . " " . date('l', strtotime($delidate)) . " this week " );
$monthly = date("Y-m-d", $dayname);
echo $monthly;

if current_time is 1 day over logged_time php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
adding one day to a date
I'm trying to add a day to a value pulled from a mysql row.
so the value getting returned is let's say
2012-10-22 22:12:13
and I want to make it
2012-11-22 22:12:13
and store it in the variable without having to interval it back into mysql and then pull it right back out.
i tried doing
$end_date_add = $enddate + 0000 . "-" . 00 . "-" . 01 . " " . 00 . ":" . 00 . ":" . 00;
with $end_date being the time logged, but it replaces the time with zeros.
am I going about this wrong?
Any help much appreciated, thank you.
This is what you want, i guess...
$date_old = strtotime("+1 MONTH", strtotime("2012-10-22 22:12:13"));
echo date("Y-m-d H:i:s", $date_old);
You can make use of strtotime to add the one month period:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo date($format, strtotime("$date +1 MONTH"));
Output (Demo):
2012-11-22 22:12:13
You can also make use of PHP's DateTime type and add a DateInterval of one day:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo (new DateTime($date))->add(new DateInterval('P1M'))->format($format);
Output (Demo):
2012-11-22 22:12:13
The code above is PHP 5.4, in PHP 5.3 you can do:
echo date_add(new DateTime($date), new DateInterval('P1M'))->format($format);
Date adding
$date = date("Y-m-d"); // current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
This could also be done as part of the MYSQL query
SELECT DATE_ADD(datecol, INTERVAL 1 DAY) AS 'datecol_plus_one'

Date manipulation issue

I want to get the start and end date of the previous calender month.
So, it's July 2012 now, I want the function to return 01 June 2012 as start and 30 June 2012 as the end.
This is my code:
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 month"));
$end_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 second"));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
But it echo's:
Start Month: 2012-07-01( 1341093600)
End Month: 2012-07-01( 1341093600)
Any idea what I'm doing wrong?
Answering whats wrong with the code you posted, you just needed to move your round bracket over a little bit and you had it. :)
<?php
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 month")));
$end_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 day")));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
?>
Try this, (check the manual of strtotime)
$now = time();
$current_month = date("Y-m-01 00:00:00", $now);
$start_month = strtotime("-1 month", $now);
$end_month = strtotime("-1 second", $now);
Try something like this:
echo date("Y-m-d", mktime(0,0,0,date('m')-1,1,date('y')));
echo date("Y-m-d", mktime(0,0,0,date('m'),0,date('y')));
Seems a little bit oversized, what you (and the other answers :X) tried. It's justs
echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));
For arbitrary months
// 3 months in the past
echo date('Y-m-d', strtotime('first day of -3 months'));
echo date('Y-m-d', strtotime('last day of -3 months'));
// 3 months in the future
echo date('Y-m-d', strtotime('first day of +3 months'));
echo date('Y-m-d', strtotime('last day of +3 months'));
Read more At the manual
echo $firstdate= "01/".date('m')."/".date('Y') ;
$lastdateofmonth=date('t',date('m'));
echo $lastdate=$lastdateofmonth."/".date('m')."/".date('Y') ;
or
$date='2012-06-05 12:00:00';
echo "End = ".date("Y-m-t",strtotime($date));
echo "start = ".date("Y-m-01",strtotime($date));

Add days to a date in PHP

Is there any php function available where I can add days to a date to make up another date? For example, I have a date in the following format:
27-December-2011
If I add 7 to the above, it should give:
03-January-2012.
Many thanks
Try this
$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));
Look at this simple snippet
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
You can use the add method of DateTime. Anyway this solution works for php version >= 5.3
date('Y-m-d', strtotime('+6 days', strtotime($original_date)));
Actually it's easier than all that.
$some_var = date("Y-m-d",strtotime("+7 day"))
You can use a variable instead of the string, of course. It will be great if the people answering the questions, won't complicate things. Less code, means less time to waste on the server ;).
$date = new DateTime('27-December-2011');
$date->add(new DateInterval('P7D'));
echo $date->format('d-F-Y') . "\n";
Change the format string to be whatever you want. (See the documentation for date()).
$registered = $udata->user_registered;
$registered = date( "d m Y", strtotime( $registered ));
$challanexpiry = explode(' ', $registered);
$day = $challanexpiry[0];
$month = $challanexpiry[1];
$year = $challanexpiry[2];
$day = $day+10;
$bankchallanexpiry = $day . " " . $month . " " . $year;

Get the date of one week from today

How do I get the date, one week from today, in the following format: YYYY-MM-DD ?
Try:
date("Y-m-d", strtotime("+1 week"));
This will output:
2015-12-31
If today is 2015-12-24
Just so Charles' prediction is wrong, here's a PHP 5.3+ example:
$now = new DateTime;
$interval = new DateInterval('P1W')
$next_week = $now->add($interval);
echo $next_week->format('Y-m-d');
or in slightly more compact form:
$now = new DateTime();
echo $now->add(new DateInterval('P1W'))->format('Y-m-d');
adding days, weeks, months to any date
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
One missing from Charles prediction, straight from the horses mouth, example #1
for ($i=0 ; $i < 7 ;$i++)
{
$date[]=date('Y-m-d',strtotime("+{$i} day",time()));
}
print_r($date);

Categories