I have problem to convert one of my dates in YYY-d-m format.
I have the array $searchQuery, which when print using print_r, prints the foloowing output:
Array ( [selectedArea] => 0
[checkin] => 30/07/2014
[checkout] => 01/08/2014
[rooms] => 1
[adults] => 2
[childrens] => 0 )
I have the following code:
echo "Checkin is:" .$searchQuery['checkin']."<br />";
$what = strtotime($searchQuery['checkin']);
echo "New checkin is:" .$newCheckin = date('Y-m-d', $what) ."<br />";
echo "Checkout is:" .$searchQuery['checkout']."<br />";
$newCheckOutTemp = strtotime($searchQuery['checkout']);
echo "New checkout is:" .$newCheckout = date('Y-m-d', $newCheckOutTemp) ."<br/>";
Which prints the following:
Checkin is:30/07/2014
New checkin is:1970-01-01 ------>????
Checkout is:01/08/2014
New checkout is:2014-01-08
I have two questions...why the first date is printing 1970-01-01 when converted, and second, how can i the difference in days between those 2 dates.
Any help will be deeply appreciated.
Regards, John
try with datetime()
$now = new DateTime(str_replace('/', '-','30/07/2014'));
echo $now->format('Y-m-d');
$newCheckOutTemp = new DateTime(str_replace('/', '-','01/08/2014'));
echo $newCheckOutTemp->format('Y-m-d');
or with date()
echo date('Y-m-d', strtotime(str_replace('/', '-',$what)));
echo $newCheckout = date('Y-m-d', strtotime(str_replace('/', '-',$newCheckOutTemp)));
For day difference here so many answers :- Date Difference in php on days?
It appears that strtotime() expects your date format to be m/d/Y. As you are passing the date starting with month 30, it doesn't know how to handle it and returns you the date 1970-01-01. If you look closely at the checkout result, it is probably not the same as you expect (8th of January instead of 1st of August).
What I would do is write a little helper function to handle all your dates.
function formatDate($date) {
return \DateTime::createFromFormat('m/d/Y', $date)->format('Y-m-d');
}
And then later use it like this:
$newCheckin = formatDate($searchQuery['checkin']);
$newCheckout = formatDate($searchQuery['checkout']);
I think you have to try: date('Y-m-d', strtotime($what)) instead of date('Y-m-d', $what)
Related
I have a problem, the date is saved in the form 1556447923594 (microdate). I want to retrieve this date from the database and display it in php.
$datetime = gmdate("Y-M-D G:i:s", $row['time']);
if($row['time'] == "0"){
echo "<td>Permban</td>";
}
else
{
echo "<td>$datetime</td>";
}
And I have = 51290-Aug-Fri 18:38:20 not this 2019-04-28 01:22:12
You just need to divide the value by 1000 to convert it to seconds so it can be used by gmdate. Also, to get the output in the form you want, you should change the format string to Y-m-d H:i:s:
$row['time'] = 1556447923594;
echo $datetime = gmdate("Y-m-d H:i:s", $row['time']/1000);
Output:
2019-04-28 10:38:43
Demo on 3v4l.org
I have a string: 30/06/18 (30th June 2018)
I am converting to a date:
$calcFieldDate = date_create_from_format('d/m/y', '30/06/18')->format('d-m-Y');
echo $calcFieldDate;
Result: 18-06-2018
Now I want to add 20 days to the date:
$expiryDate = date("d-m-Y", strtotime("+20 days", $calcFieldDate));
echo $expiryDate;
Expected Result: 08-07-2018
Actual Result: 31-01-1970
I am obviously creating a date format which is then subsequently being treated as a string...
Every time I try a conversion, I just hit another road block - is there anyway to create a date that is then treated like a date?
$calcFieldDate = date_create_from_format('d/m/y', '30/06/18')->format('d-m-Y');
echo $calcFieldDate;
Result:30-06-2018
$expiryDate = date("d-m-Y", strtotime("+20 days", strtotime($calcFieldDate)));
echo $expiryDate;
Result:20-07-2018
Strtotime() The second parameter is the timestamp
You actually don't need to revert using strtotime and date functions, you can actually use DateTime to simply add dates into it:
$calcFieldDate = date_create_from_format('d/m/y', '30/06/18');
echo $calcFieldDate->format('d-m-Y'); // get inputted date
$expiryDate = clone $calcFieldDate; // clone the original date object
$expiryDate->modify('+20 days'); // adjust the cloned date
echo $expiryDate->format('d-m-Y'); // show the adjusted date
This will sort your problem.
$str="30/06/18 (30th June 2018)";
$arr_temp=explode(" ",$str);
$str_date=str_replace("/","-",$arr_temp[0]);
$dt = DateTime::createFromFormat('d-m-y',$str_date);
$date=$dt->format('d-m-Y');
$new_date=date('d-m-Y',strtotime("+20 days",strtotime($date)));
echo $new_date;
I am using the following code to get the difference between two dates. I'm having issues with it returning the correct amount of days left.
<?php
$nextservicedate = FrmProDisplaysController::get_shortcode( array( 'id' => 3451 ) );
$currentdate = date("d/m/Y");
$daysremaining = $nextservicedate - $currentdate;
echo $nextservicedate. " | ";
if ( strpos($nextservicedate, 'None registered') !== false )
{
echo "None Registered";
}
elseif ($daysremaining < "0")
{
$negativedays = str_replace('-', ' ', $daysremaining);
echo $negativedays. " days overdue";
}
elseif ($daysremaining <= "30")
{
echo $daysremaining. " days (upcoming service)";
}
else
{
echo $daysremaining. " days";
}
?>
The entry
FrmProDisplaysController::get_shortcode( array( 'id' => 3451 ) )
returns a date from a wordpress form plugin (FormidablePro) as 30/10/2016.
The code is returning there are 23 days remaining which I believe is taking it to the end of this month.
I know I'm missing something and think it probably has something to do with the working out the days remaining part of the code.
Can anyone see any glaring errors? Do I need to declare the $nextservicedate like I've done with $currentdate?
Any help would be greatly recieved!
Regards
Matt
PHP's date() returns date string which cannot be used to perform arithmetic operations on date.
What you need to do is create DateTime object of service date from String using createFromFormat() and create current DateTime() object.
Then use diff() function of DateTime object to find the difference between two days.
So your code would look something like this,
$nextservicedate=DateTime::createFromFormat("d/m/Y",$nextservicedate);
$todaydate=new DateTime();
$difference=$nextservicedate->diff($todaydate);
To get the difference in days,
echo $difference->format('%R%a days');
I am passing a date in a URL in a UK format as per the following:
http://www.website.com/index.php?from_date=01/04/2013&to_date=12/04/2013
The date range is 1st April 2013 to 12th April 2013.
Then I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$rpt_from_date = date("Y-m-d", strtotime($_GET["from_date"]) );
$rpt_to_date = date("Y-m-d", strtotime($_GET["to_date"]) );
echo $rpt_from_date;
echo "</br>";
echo $rpt_to_date;
?>
For some reason this is not working. The above returns the following:
2013-01-04
2013-12-04
It's switching the month and day around. I want it to return:
2013-04-01
2013-04-12
Does anyone have any ideas?
Use DateTime object, to get php understand in which format you passing date to it.
$rpt_from_date = DateTime::createFromFormat('d/m/Y', $_GET["from_date"]);
echo $rpt_from_date->format('Y-m-d');
PHP is reading your time string in the US format (MM/DD/YYYY) because you are using slashes. You could use dashes to give the time: index.php?from_date=01-04-2013&to_date=12-04-2013, or convert it yourself:
$uktime = implode("-", explode("/", $_GET['from_date']));
I will provide the solution but it is not using the date function:
$arr = explode("/",$_GET["from_date"]);
$from_date = $arr[2]."-".$arr[1]."-"$arr[0];
Second solution is as following:
$from_date = implode(array_reverse(explode("/",$_GET["from_date"])));
EDIT
Per the comment below, here's my attempt at splitting / combining / converting the string prior to implementing mktime...
$date1 = explode("T",'2005-03-27T00:00:00');
$date2 = explode("-", $date1[0]);
$try = mktime($time1,$time2,0,$date2[1],$date[2],$date[3]);
print $try;
// Prints out: 951793200
Original Question:
I've inherited a database and would like very much to convert the bizarre way the data is stored to something more mysql-friendly...
In the meantime, I get a text string (yes... text)... That I'd like to convert to unixtime...
So, I'll get a string that looks like this:
2005-03-27T00:00:00 03:00 AM
I'd like to convert it to:
1111885200
Dates and times always mess me up... I've done a number of things using strtotime and mktime, but can't get it formatted the way I want.
Well, this is how I would do it.
$ex = '2005-03-27T00:00:00 03:00 AM';
$format = '%Y-%m-%dT00:00:00 %H:%M %p';
$strf = strptime($ex, $format);
$date_str = $strf['tm_mon'] + 1 . '/' . $strf['tm_mday'] . '/' . ($strf['tm_year'] + 1900) . ' ' . $strf['tm_hour'] . ':' . $strf['tm_min'] . ':00';
echo $date_str;
echo "\n";
echo strtotime($date_str);
echo "\n";
echo date('m-d-Y H:i:s', 1111885200);
But, your desired result does not seem to be correct based on the date you posted.
OUTPUT
3/27/2005 3:0:00
1111921200
03-26-2005 17:00:00
You could write a litte converter script that does the task for you.
Please check the optional parameters of mktime
You could try to split your string in hour / minute / second / month / day / year and put that into mktime. Then mktime will give you the unix timestamp for that.
So, I'm not a fan of regular expressions unless absolutely necessary, but they might be necessary here to pick out the time zone piece.
preg_match("/(\d{4})-(\d{2})-(\d{2})(T\d{2}:\d{2}:\d{2}) (\d{2}:\d{2}) (AM|PM)/", '2005-03-27T00:00:00 03:00 AM',$matches);
Will give you $matches that look something like:
Array
(
[0] => 2005-03-27T00:00:00 03:00 AM
[1] => 2005
[2] => 03
[3] => 27
[4] => T00:00:00
[5] => 03:00
[6] => AM
)
I would feed those pieces into a DateTime object. Then you can output it into any format you want. You also may have to adjust that regex some so it can handle all your dates.
If the T00:00:00 part is useless, just remove it and use strtotime():
<?php
$date = '2005-03-27T00:00:00 03:00 AM';
$timestamp = strtotime(preg_replace('!T[^ ]+!', '', $date));
var_dump(date('d/m/Y H:i:s', $timestamp));
?>
This prints:
string(19) "27/03/2005 03:00:00"