How to get dates based on current day in PHP? - php

I want to get 4 different dates with PHP: 1 day, 1 week, 1 month and 3 months.
Every date is from the current day, so lets say today is 2014-11-26.
get past dates based on current date.
$today = date("Y-m-d");
$one_day = $today - //how do I get yesterday from current day?
$one_week = $today - //how do I get one week from current day?
$one month = $today - //how do I get one month from current day?
$three_month = $today - //how do I get three month from current day?

The strtotime()-function is perfect for that approach.
Use something like
echo date( 'Y-m-d', strtotime("-1 day"));
echo date( 'Y-m-d', strtotime("-1 week"));
echo date( 'Y-m-d', strtotime("-1 month"));
echo date( 'Y-m-d', strtotime("-1 year"));

You can use the DateTime class:
$date = new DateTime('now');
echo $date->format('Y-m-d');
$date = new DateTime('-1 day');
echo $date->format('Y-m-d');
$date = new DateTime('-1 week');
echo $date->format('Y-m-d');
$date = new DateTime('-1 month');
echo $date->format('Y-m-d');
$date = new DateTime('-3 months');
echo $date->format('Y-m-d');
See Supported Date and Time Formats for more details.

Related

How to get Last sunday date in Bootstrap Datepicker?

I am using bootstrap Datepicker and I want to set the date for current/last Sunday.The code below is showing the content form $date to $date1.
if (!isset($_POST['new-date'])){
$date = date('Y-m-d H:i:s');// I want to set last/current sunday
date here.
$date1 = date("Y-m-d H:i:s", strtotime("$date +6 day"));
$d = explode(" ",$date);
$d1 = explode(" ",$date1);
} else {
$d[0] = $_POST['new-date'];
$d1[0] = date("Y-m-d", strtotime("$d[0] +6 day"));
}
$d[0] = $_POST['new-date'];
The ['new-date'] may be any day/date from the calendar but It should select the last Sunday of that particular week.
Your code seems to be written in PHP, rather than JavaScript, so I'm not sure how relevant the question title or tags are.
// Check if the day of the week is a Sunday. If yes, use today's date. If no, use last Sunday's date
$timestamp = date('N') == 7 ? strtotime('today') : strtotime('last sunday');
$date = date('Y-m-d H:i:s', $timestamp);
die($date);
Edit
You asked in the comments how you can find last Sunday for any given date.
$timestamp = strtotime($_POST['date'] ?? 'today');
$sunday = (date('N', $timestamp) == 7) ? $timestamp : strtotime('last sunday', $timestamp);
$date = date('Y-m-d H:i:s', $sunday);
die($date);

how to get next month's date and it's not related to current month date in MySQL

I have three columns in my table, future 3 months like fistdate(next month), secondate(+2 next month) and thirdate(+3 next month) which are DATE types. I want to save my dates as follows 09/09/2017, 09/10/2017 and 09/11/2017 and current month is 07/08/2017 and above 3 dates are not related to current month date like 07/08/2017
I have tried following code but it is related to current month
$date1 = date('Y-m-d', strtotime('+1 month'));
$date2 = date('Y-m-d', strtotime('+2 month'));
$date3 = date('Y-m-d', strtotime('+3 month'));
$install = array(
"invoiceid" =>$this->input->post('salesInvoiceno', TRUE),
"fistdate" =>$date1,
"secondate" =>$date2,
"thirdate" =>$date3
);
$time = strtotime("2010-08-06");$date1 = date('Y-m-d', strtotime('+1 month',$time));$date2 = date('Y-m-d', strtotime('+2 month',$time));$date3 = date('Y-m-d', strtotime('+3 month',$time));echo $date1.' '.$date2.' '.$date3;

How to get the current date in PHP, and add 1 month to the current date?

I'm coding a script where I require to save the current date, and the date 1 month from that date. I am pretty sure that the time() variable works, but I am not sure how to +1 month onto that?
Any ideas, suggestions. Cheers!
Try this
$today = date("Y-m-d");
$date = date('Y-m-d', strtotime('+1 month', $today));
or use DateTime()
$dt1 = new DateTime();
$today = $dt1->format("Y-m-d");
$dt2 = new DateTime("+1 month");
$date = $dt2->format("Y-m-d");
$time = strtotime("2010-12-11");
$final = date("Y-m-d", strtotime("+1 month", $time));
(OR)
strtotime( "+1 month", strtotime( $time ) );
this returns a timestamp that can be used with the date function
Use this:
Current Date:
echo "Today is " . date("Y/m/d");
1 Month to the Current Date:
$time = strtotime(date("Y/m/d"));
$final = date("Y-m-d", strtotime("+1 month", $time));
<?php
$current_time = date("Y-M-d h:i:s",time()); // Getting Current Date & Time
print $current_time; // Current Date & Time Printing for display purpose
$future_timestamp = strtotime("+1 month"); // Getting timestamp of 1 month from now
$final_future = date("Y-M-d h:i:s",+$future_timestamp); // Getting Future Date & Time of 1 month from now
print $final_future; // Printing Future time for display purpose
?>
shorter : $today=date("Y-m-d"); $date=
This one liner worked for me:
$monthFromToday = date("Y-m-d", strtotime("+1 month", strtotime(date("Y/m/d"))));
The given answers may not give you the results you might expect or desire.
Consider:
$today = "29Jan2018";
$nextMonth = date('dMY', strtotime('+1 month', (strtotime($today))));
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018
$today = date("Y-m-d");
$enddate = date('Y-m-01',strtotime($today. ' + 1 months'));
You could also consider using the Carbon package.
The solution would look like this:
use Carbon\Carbon
$now = Carbon::now;
$now->addMonth();
Here is the link for reference https://carbon.nesbot.com/docs/

Get timestamp of today and yesterday in php

How can I get the timestamp of 12 o'clock of today, yesterday and the day before yesterday by using strtotime() function in php?
12 o'clock is a variable and would be changed by user.
$hour = 12;
$today = strtotime($hour . ':00:00');
$yesterday = strtotime('-1 day', $today);
$dayBeforeYesterday = strtotime('-1 day', $yesterday);
strtotime supports a number of interesting modifiers that can be used:
$hour = 12;
$today = strtotime("today $hour:00");
$yesterday = strtotime("yesterday $hour:00");
$dayBeforeYesterday = strtotime("yesterday -1 day $hour:00");
echo date("Y-m-d H:i:s\n", $today);
echo date("Y-m-d H:i:s\n", $yesterday);
echo date("Y-m-d H:i:s\n", $dayBeforeYesterday);
It works as predicted:
2011-01-24 12:00:00
2011-01-23 12:00:00
2011-01-22 12:00:00
OO Equivalent
$iHour = 12;
$oToday = new DateTime();
$oToday->setTime($iHour, 0);
$oYesterday = clone $oToday;
$oYesterday->modify('-1 day');
$oDayBefore = clone $oYesterday;
$oDayBefore->modify('-1 day');
$iToday = $oToday->getTimestamp();
$iYesterday = $oYesterday->getTimestamp();
$iDayBefore = $oDayBefore->getTimestamp();
echo "Today: $iToday\n";
echo "Yesterday: $iYesterday\n";
echo "Day Before: $iDayBefore\n";
You can easily find out any date using DateTime object, It is so flexible
$yesterday = new DateTime('yesterday');
echo $yesterday->format('Y-m-d');
$firstModayOfApril = new DateTime('first monday of april');
echo $firstModayOfApril->format('Y-m-d');
$nextMonday = new DateTime('next monday');
echo $nextMonday->format('Y-m-d');
to get start of day yesterday
$oDate = new DateTime();
$oDate->modify('-1 day');
echo $oDate->format('Y-m-d 00:00:00');
result
2014-11-05 00:00:00
All the answers here are too long and bloated, everyone loves one-lines ;)
$yesterday = Date('Y-m-d', strtotime('-1 day'));
(Or if you are American you can randomize the date unit order to m/d/y (or whatever you use) and use Cups, galloons, feet and horses as units...)
As of PHP 7 you can write something like this:
$today = new \DateTime();
$yesterday = (clone $today)->modify('-1 day');
$dayBefore = (clone $yesterday)->modify('-1 day');
// Then call ->format('Y-m-d 00:00:00'); on each objects
you can also use new DateTime("now") for today new DateTime("1 day ago") for yesterday or all can be parse by strtotime php function.
Then format as you want.
$timeStamp = time();
// $timeStamp = time() - 86400;
if (date('d.m.Y', $timeStamp) == date('d.m.Y')) {
echo 'Today';
} elseif (date('d.m.Y', $time) == date('d.m.Y', strtotime('-1 day'))) {
echo 'Yesterday';
}

The first day of the current month in php using date_modify as DateTime object

I can get the Monday of this week with:
$monday = date_create()->modify('this Monday');
I would like to get with the same ease the 1st of this month. How can I achieve that?
Here is what I use.
First day of the month:
date('Y-m-01');
Last day of the month:
date('Y-m-t');
Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3). Otherwise the example above is the only way to do it:
<?php
// First day of this month
$d = new DateTime('first day of this month');
echo $d->format('jS, F Y');
// First day of a specific month
$d = new DateTime('2010-01-19');
$d->modify('first day of this month');
echo $d->format('jS, F Y');
// alternatively...
echo date_create('2010-01-19')
->modify('first day of this month')
->format('jS, F Y');
In PHP 5.4+ you can do this:
<?php
// First day of this month
echo (new DateTime('first day of this month'))->format('jS, F Y');
echo (new DateTime('2010-01-19'))
->modify('first day of this month')
->format('jS, F Y');
If you prefer a concise way to do this, and already have the year and month in numerical values, you can use date():
<?php
echo date('Y-m-01'); // first day of this month
echo "$year-$month-01"; // first day of a month chosen by you
This is everything you need:
$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());
$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());
$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());
echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';
echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';
echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
Currently I'm using this solution:
$firstDay = new \DateTime('first day of this month');
$lastDay = new \DateTime('last day of this month');
The only issue I came upon is that strange time is being set. I needed correct range for our search interface and I ended up with this:
$firstDay = new \DateTime('first day of this month 00:00:00');
$lastDay = new \DateTime('first day of next month 00:00:00');
I use a crazy way to do this is using this command
$firstDay=date('Y-m-d',strtotime("first day of this month"));
$lastDay=date('Y-m-d',strtotime("last day of this month"));
Thats all
In php 5.2 you can use:
<? $d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') ?>
Ugly, (and doesn't use your method call above) but works:
echo 'First day of the month: ' . date('m/d/y h:i a',(strtotime('this month',strtotime(date('m/01/y')))));
You can do it like this:
$firstday = date_create()->modify('first day January 2010');
using date method, we should be able to get the result.
ie; date('N/D/l', mktime(0, 0, 0, month, day, year));
For Example
echo date('N', mktime(0, 0, 0, 7, 1, 2017)); // will return 6
echo date('D', mktime(0, 0, 0, 7, 1, 2017)); // will return Sat
echo date('l', mktime(0, 0, 0, 7, 1, 2017)); // will return Saturday
I use this with a daily cron job to check if I should send an email on the first day of any given month to my affiliates. It's a few more lines than the other answers but solid as a rock.
//is this the first day of the month?
$date = date('Y-m-d');
$pieces = explode("-", $date);
$day = $pieces[2];
//if it's not the first day then stop
if($day != "01") {
echo "error - it's not the first of the month today";
exit;
}
Timestamp for start of this month and very last second of current month.
You can add 00:00:00 or just reference "today"
Alternative:
$startOfThisMonth = strtotime("first day of this month",strtotime("today"));
OR
$startOfThisMonth = strtotime("first day of this month 00:00:00");
$endOfThisMonth = strtotime("first day of next month",$startOfThisMonth)-1;
I am providing this answer as an alternative one liner if the DateTime object is not preferred
Basically, I get the current day number, reduce it by one then take that number of days from itself ("today" which automatically resets the clock to 00:00:00 too) and you get the start of the month.
$startOfMonth = strtotime("today - ".(date("j")-1)." days");
If you're using composer, you can install carbon:
composer require nesbot/carbon
This is then as simple as:
use Carbon/Carbon;
$startOfMonth = Carbon::now()->startOfMonth()->toDateTime();

Categories