date_create function is not working in php - 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

Related

Comparing post date get_the_date() from today - Wordpress

I want to be able to calculate the days that post was created and compare with today to echo "Today/ Yesterday/ Last Week/ Last Month". The date format I get from get_the_date() is "December 1, 2015" so I'm wondering if I need to use a different function that I don't know about.
You just need to the get_the_date() function;
Now Date Should In YYYY-MM-DD Format
for that
$date1 = date('Y-m-d', strtotime(get_the_date())) ;
$current_date1 = date('Y-m-d', time()) ;
Now use this function
function dateDifference($date_1 , $date_2 )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format('%a');
}
//call above function
echo $days = dateDifference($date1, $current_date1);
I'm not sure if there is any WordPress function but you can get your values using built in PHP functions.
Yesterday:
date('Y-m-d', strtotime("-1 day"));
Last week
date('Y-m-d', strtotime("-1 week +1 day"));
Last month
date('Y-m-'.1, strtotime("-1 month")); //First day of -1 month
You can read more about strtotime here http://php.net/manual/en/function.strtotime.php
Here is also a link to the date function, if you havent had any experience with it before: http://php.net/manual/en/function.date.php
You would want to use Y-m-d format for wordpress querys read more about that here: http://php.net/manual/en/function.date.php

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 -

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

PHP Reduce days, hours and minutes from a datetime

Need to reduce no of days, hours and minutes from a datetime using php.
Datetime is of the format Y-m-d H:i:s
eg: Suppose datetime is 2013-03-20 14:20:00. How to reduce 2 days , 3 hours and 10 minutes from this such that it results in 2013-03-18 11:10:00.
<?php
$date = new DateTime("2013-03-20 14:20:00");
$dateIncremented = $date->sub(date_interval_create_from_date_string('2 days 3 hours 10 minutes'));
$finalDate = $date->format("Y-m-d H:i:s");
echo $finalDate;
?>
Allirght. An Alias. But Readable format
use the DateTime object :
$date = new DateTime('2013-03-20 14:20:00');
$date->sub(new DateInterval('P2DT3H10M'));
echo $date->format('Y-m-d H:i:s');
You must explore DateTime::sub and DateInterval, and about DateInterval format
echo \DateTime::createFromFormat('Y-m-d H:i:s', '2013-03-20 14:20:00')
->sub(new \DateInterval('P2DT3H10M'))
->format('Y-m-d H:i:s'); // 2013-03-18 11:10:00

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