build a new mysql variable from an existing date field - php

I have a date column in my MySQL table and I want to build a new variable from that whereby I will add a few days.
example:
$date = 2014-12-12
now I need a second variable $date2 whereby the date will be 2014-12-17
So I need something like this
$date2 = $date + 5 days
I've searched for this and I got solutions for building queries, but I want to have a second variable. Is this possible?
I tried this (with no luck)
$date2 = DateTime::createFromFormat('d-m-Y', $date1);
$date2->modify('5 day');
echo $date2->format('Y-m-d');

This should work:
$date2 = strtotime($date) + (60*60*24*5); //convert date to unix time stamp and add 5 days
$date2 = date('Y-m-d', $date2); //convert back to readable format
Or an even better approach:
$date2 = date('Y-m-d', strtotime($date . "+5 days"));

You are missing the + in your modify call:
$date2 = DateTime::createFromFormat('d-m-Y', $date1);
$date2->modify('+5 day');
echo $date2->format('d-m-Y');

Related

date_create function is not working in php

I have an issue.I need to calculate the time difference but here the date_create function is not showing any output in php.I am explaining my code below.
while($row=mysqli_fetch_assoc($orderqry)){
$exit_date=$row['delivered_datetime'];
if($exit_date!=='0000-00-00 00:00:00'){
$deldate=date_create($exit_date);
$today=date_create(date('Y-m-d H:i:s'));
echo $today;exit;
$interval = date_diff($deldate, $today);
$hours = $interval->format('%h');
}
}
Here i am trying to echo $today but its not giving any result.Here i need to calculate the time difference in hour.In my DB i have existing time in the format of date('Y-m-d H:i:s').Please help me.
$today is DateTime instance. To echo it you need to call its format() method.
echo $today->format('Y-m-d H:i:s');
date_create is an alias of: DateTime::__construct(). see below example, I have assigned 6 Hour ago date in $exit_date. so ideally difference from current date should be 6 Hour.
$exit_date = date('Y-m-d H:i:s', strtotime('-6 Hours'));
$deldate=date_create($exit_date); //returns datetime class object
$today=date_create(date('Y-m-d H:i:s'));
$interval = date_diff($deldate, $today);
echo $hours = $interval->format('%h'); // output 6 hours
If you want to print date from $today variable then use:
echo $today->format('Y-m-d H:i:s'); //output: today's date in Y-m-d H:i:s format
for more detail have a look at http://php.net/manual/en/book.datetime.php

PHP - Compare Dates with different format

Date Format:
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
How can I check if
$date1 <= $date2 || $date1 => $date2
Do I need to convert date format in
$date1 = '16-3-2015';
$date2 = '04-2-15';
Use strtotime():
<?php
$date1 = strtotime("16-MAR-2015");
$date2 = strtotime("04-FEB-15");
?>
and compair
Try this way, it works. DEMO
$date1 = DateTime::createFromFormat('j-M-Y', '16-MAR-2015');
$date2 = DateTime::createFromFormat('j-M-y', '04-FEB-15');
$date1=$date1->format('Y-m-d');
$date2=$date2->format('Y-m-d');
var_dump($date1 <= $date2);
var_dump($date1 >= $date2);
You don't need to convert anything with the given formats, DateTime will do what you need.
Just turn them into DateTime objects and compare them :
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
$date1 = date_create($date1);
$date2 = date_create($date2);
// $date1 > $date2 -> true
// $date1 < $date2 -> false
Or
if (date_create($date1) > date_create($date2)) {
// Use $date1 unchanged
}
First, you need to turn them into a DateTime object.
DEMO
$date1 = '16-MAR-2015';
$date2 = '04-FEB-15';
$dateComp1 = new DateTime($date1);
$dateComp2 = new DateTime($date2);
Then you can compare them, DateTime is smart enough to convert the format automatically.
$myDateTime = DateTime::createFromFormat('Y-m-d', $dateString); $newDateString = $myDateTime->format('m/d/Y'); There is a magical function called strtotime()
If you need to print the value in a readable format you can use the function DATE
date("d-m-Y h:i:s",$date1);
In your case.
You want to use
Date.parse()
try
date1= new DateTime();
date1= new Datetime->format('d-m-Y'); // this will print year 4 digits ie; 2017
date2= new DateTime();
date2= new DateTime->format('d-m-y'); //this will print 2 digit year ie; 17
// to compare
$differance=$date1->diff($date2);
echo $differance->format('d-m-y'); //or change to upper case Y for 4 digit year
note... in mysql 5.6 it seems the input format must be Y first: if you insert it with Y-first format first, it seems to compare correctly no matter what the second format is.

Setting Php and Mysql timestamp

I need to set timestamp eg. 4 hours ahead and 2 hours ahead separately
In my database, I have their columns as timestamp.
I know I could do something similar to this but am not sure if it's correct.
// For 4 hours ahead of time
$dt2 = date("Y-m-d 04:i:s");
//For 2 days ahead
$dt2 = date("Y-m-02 H:i:s");
//For 4 hours ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+4 hours'));
//For 2 days ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+2 days'));
In my mind it's much better to work with DateTime field and the DateTime class.
You have the ability so modify that objects very easily. For example:
$aktDate = new \DateTime();
Now you have the actual date and time in an object. If you want you can put a string insight the DateTime function so set your date manually.
$aktDate = new \DateTime('Y-m-d 04:i:s');
Not you can modify your dates if you want with the modify function.
in your case:
$pastDate = clone $aktDate;
$pastDate->modify('+2 days');
$futureDate = clone $aktDate;
$futureDate->modify('+4 days');
if($pastDate < $aktDate && $aktDate < $futureDate) {
// do something
}
I like the DateTime function much more because it's readable and you can work directly with your DateTime fields from your MySQL database if you have such fields. You can write that example much shorter but so you have better readability.
$date = new DateTime('now');
$date->modify('+2 days');
echo $date->format('Y-m-d H:i:s');
$date = new DateTime('now');
$date->modify('+4 hours');
echo $date->format('Y-m-d H:i:s');
You need to use the strtotime() function (http://php.net/manual/en/function.strtotime.php).
For your examples:
//+2 hours<br>
strtotime("+2 hours");
// +2 days<br>
strtotime("+2 days")
Edit: for what you ask, about posted values, the syntax is like this:
strtotime("+2".$_POST['field_name']." days");
You can use hours/days/months/weeks/years and either + or -

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/

adding 1 day to a DATETIME format value

In certain situations I want to add 1 day to the value of my DATETIME formatted variable:
$start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));
What is the best way to do this?
There's more then one way to do this with DateTime which was introduced in PHP 5.2. Unlike using strtotime() this will account for daylight savings time and leap year.
$datetime = new DateTime('2013-01-29');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.3
$datetime = new DateTime('2013-01-29');
$datetime->add(new DateInterval('P1D'));
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.4
echo (new DateTime('2013-01-29'))->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
// Available in PHP 5.5
$start = new DateTimeImmutable('2013-01-29');
$datetime = $start->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
If you want to do this in PHP:
// replace time() with the time stamp you want to add one day to
$startDate = time();
date('Y-m-d H:i:s', strtotime('+1 day', $startDate));
If you want to add the date in MySQL:
-- replace CURRENT_DATE with the date you want to add one day to
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);
The DateTime constructor takes a parameter string time. $time can be different things, it has to respect the datetime format.
There are some valid values as examples :
'now' (the default value)
2017-10-19
2017-10-19 11:59:59
2017-10-19 +1day
So, in your case you can use the following.
$dt = new \DateTime('now +1 day'); //Tomorrow
$dt = new \DateTime('2016-01-01 +1 day'); //2016-01-02
Use strtotime to convert the string to a time stamp
Add a day to it (eg: by adding 86400 seconds (24 * 60 * 60))
eg:
$time = strtotime($myInput);
$newTime = $time + 86400;
If it's only adding 1 day, then using strtotime again is probably overkill.
You can use
$now = new DateTime();
$date = $now->modify('+1 day')->format('Y-m-d H:i:s');
You can use as following.
$start_date = date('Y-m-d H:i:s');
$end_date = date("Y-m-d 23:59:59", strtotime('+3 days', strtotime($start_date)));
You can also set days as constant and use like below.
if (!defined('ADD_DAYS')) define('ADD_DAYS','+3 days');
$end_date = date("Y-m-d 23:59:59", strtotime(ADD_DAYS, strtotime($start_date)));
I suggest start using Zend_Date classes from Zend Framework. I know, its a bit offtopic, but I'll like this way :-)
$date = new Zend_Date();
$date->add('24:00:00', Zend_Date::TIMES);
print $date->get();
Using server request time to Add days. Working as expected.
25/08/19 => 27/09/19
$timestamp = $_SERVER['REQUEST_TIME'];
$dateNow = date('d/m/y', $timestamp);
$newDate = date('d/m/y', strtotime('+2 day', $timestamp));
Here '+2 days' to add any number of days.
One liner !
echo (new \DateTime('2016-01-01 +1 day'))->format('Y-m-d H:i:s');

Categories