I want to convert my datetime 08/11/2013 to 2013-08-11.
I was trying this, but it did not work as it suppose to.
$date = $_POST['gt_date']; // Getting 08/11/2013
$date = explode("/", $date); // Exploding the / character into array
$datetime = $date[2] + '-' + $date[0] + '-' + $date[1];
Any Ideas?
You can pass it to DateTime to format it for you.
$date = new DateTime('08/11/2013');
echo $date->format('Y-m-d');
I don't know what the + does, but changing your code to the following will work:
$datetime = $date[2].'-'.$date[0].'-'.$date[1];
Edit: I guess the + was adding them together. Was the result you were getting 2032?
Here's a function that will return your format so that you don't have to repeat your code.
function makeDateFormat($date) {
$date = $_POST['gt_date'];
$date = explode("/",$date);
$new_date = array($date[2], $date[0], $date[1]);
$date = implode("-",$new_date);
return $date;
}
This issue is caused by the "+" operator, replace it with "." operator. You could use this function:
function formatDate($date) {
$arr = explode("-",$date);
$year = $arr[2];
$month = $arr[1];
$day = $arr[0];
return $year."-".$month."-".$day;
}
Nice function but correction is this below if you want to covert format as well:
echo makeDateFormat('13/02/23','Y-m-d');
function makeDateFormat($date,$date_format) {
$date = explode("/",$date);
$new_date = array($date[2], $date[1], $date[0]);
$date = implode("-",$new_date);
$date=date($date_format,strtotime($date));
return $date;
}
Another way if your require to format date as per your requirement:
$date=explode("/","13/02/23");
$new_date_format=date('Y-m-d',strtotime($date[2].'-'.$date[1].'-'.$date[0]));
Related
I'm trying to convert strings with hungarian datetime format, but no success because of the dot-separators:
<?php
$dtime = DateTime::createFromFormat("YY'.'MM'.'DD HH:MM:II frac", "2020.07.22 22:41:36.258");
$timestamp = $dtime->getTimestamp();
echo("Result: " . $timestamp . "<br>");
?>
Isn't it possible without "string-replace" like this:
strtotime(preg_replace("/([0-9]{4})\.([0-9]{2})\.([0-9]{2})/",'${1}-${2}-${3}',$xml->delivery_time)) ?
(I'm new to PHP 5 and I'm shocked it can not simply convert a common date format. Searched 200+ results, wasted 4+ hours ... no success.)
The correct format is stored in the $format variable:
(Note: the v (millisec) modifier has only been added since v7.3)
<?php
$format = 'Y.m.d H:i:s.v';
$dtime = DateTime::createFromFormat($format, "2020.07.22 22:41:36.258");
$timestamp = $dtime->getTimestamp();
echo("Result: " . $timestamp . "<br>");
?>
Result: 1595457696
This solution will also work for PHP versions below 7.3
// convert a hungarian datetime to a timestamp
function toTimestamp($dt)
{
$format = 'Y.m.d H:i:s.';
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
$dt = explode('.', $dt);
$dt[3] = intval($dt[3] * 1000);
$dt = implode('.', $dt);
$format .= 'u';
} else {
$format .= 'v';
}
return DateTime::createFromFormat($format, $dt)->getTimestamp();
}
$timestamp = toTimestamp('2020.07.22 22:41:36.258');
var_dump($timestamp);
I am using Carbon to add number of days, it there a way to avoid using for and/or while loop?
Add the numbers of days ($skipDayBy) and add the number of days if found in $excludeDatesPublic or $excludeDatesManual?
For example working demo:
function calculateDate($skipDayBy = 0) {
$excludeDatesPublic = ['2019-08-28'];
$excludeDatesManual = ['2019-09-01'];
$date = Carbon::now();
for($i = 0; $i < $skipDayBy; $i++) {
$date = $date->addDays(1);
while(in_array($date->toDateString(), $excludeDatesPublic) || in_array($date->toDateString(), $excludeDatesManual))
{
$date = $date->addDays(1);
}
}
return $date->toDateString();
}
echo calculateDate(4);
Returned 2019-09-02 as expected if today date is 2019-08-27.
Maybe you're looking for https://github.com/kylekatarnls/business-day that allows you to add days skipping holidays.
Alternatively, you can use the periods:
$skipDayBy = 5;
$excludeDatesPublic = ['2019-09-01'];
$excludeDatesManual = ['2019-09-04'];
$exclude = array_merge($excludeDatesPublic, $excludeDatesManual);
$date = CarbonPeriod::create(Carbon::now(), $skipDayBy)
->addFilter(function (Carbon $date) use ($exclude) {
return !in_array($date->format('Y-m-d'), $exclude);
})
->calculateEnd();
var_dump($date); // 2019-09-06 18:50:17 if run at 2019-08-31 18:50:17
With PHP shortly
$day='2019-12-12';
$date = date('Y-m-d', strtotime($day . " +4 days"));
echo $date;
Output
2019-12-16
Or u can use
$date= date('Y-m-d', strtotime('+4 days', strtotime($day)));
I need to Write a function named countDays which takes a single parameter named dateinstring which is string in the form ”MM.DD.YYY” represent a real date value. The function should print to the console the number of days from the beginning of the year specified in dateInString until the date represented in dateInString. If the value of dateInString is invalid, the function should print ”Bad format” to the console.
I have written the code as below :
function countDays($dateInString){
date_default_timezone_set('America/Los_Angeles');
$date = explode('.', $dateInString);
if(count($date) == 3 && checkdate($date[0], $date[1], $date[2])){
$formatted_date = $date[2].'-'.$date[0].'-'.$date[1].'00:00:00';
$diff = strtotime($formatted_date).'-'.strtotime($date[2].'-01-01 00:00:00');
echo round($diff/86400)+1;
}
else {
echo 'Bad format';
}
};
countDays('1.15.2014');
But the above code seems that not giving the correct output. It is about 33% correct. But where is the problem with this code ? Please help me!!!
$diff = strtotime($formatted_date).'-'.strtotime($date[2].'-01-01 00:00:00');
Change to
$diff = strtotime($formatted_date) - strtotime($date[2].'-01-01 00:00:00');
You made the minus symbol a string instead of an operator.
You could try it this way
function countDays($dateInString) {
date_default_timezone_set('America/Los_Angeles');
$date = explode('.', $dateInString);
if (checkdate($date[0], $date[1], $date[2])) {
$year_start = mktime(0, 0, 0, 1, 1, $date[2]);
$your_date = mktime(0,0,0,$date[0], $date[1], $date[2]);
$diff = $your_date - $year_start;
echo floor($diff /(60*60*24));
} else {
echo "Bad date supplied";
}
}
A better approach would be to use the DateTime class. I haven't included the validation in this, but i suggest you use regex for that.
function countDays($dateInString){
$parts = explode('.', $dateInString);
$date = new DateTime($parts[2] . '-' . $parts[0] . '-' . $parts[1]);
$compare = new DateTime( $date->format('Y') . '-01-01' );
$interval = $date->diff($compare);
return $interval->format('%a');
}
echo countDays('09.15.2014');
Check this out.
function countDays($dateInString){
date_default_timezone_set('America/Los_Angeles');
$date = explode('.', $dateInString);
if(count($date) == 3 && checkdate($date[0], $date[1], $date[2])){
$formatted_date = strtotime($date[2].'/'.$date[0].'/'.$date[1]);
$endTimeStamp = strtotime("2014/01/01");
$timeDiff = abs($endTimeStamp - $formatted_date);
echo round(intval($timeDiff/86400));
}
else {
echo 'Bad format';
}
};
countDays('01.01.2014');
I am making a function that remembers a users birthday, and im storing the birthdate in my database in this format "08/12/1988".
Now how do I remove the "YYYY", so that I end up with only day and month? 08/12
Because that would allow me to make a if statement like this:
THIS DIDN'T WORK:
$date_today = date("d-m");
if($string_from_db_without_year == $date_today){
echo"Congrats, it's your birthday!";
}
THIS WORKED:
$string_from_db = $bruger_info_profil['birthday'];
$date = strtotime($string_from_db);
$converted_date_from_db = date('m/d',$date);
echo $converted_date_from_db;
<?php
$date = strtotime('08/12/1988');
echo date('d/m',$date);
?>
Try with:
$input = '08/12/1988';
$output = date('d-m', strtotime($input));
$time = strtotime( "08/12/1988");
$birthday = date("d-m", $time);
Or
list($month, $day, $year) = explode('/', "08/12/1988");
$birthday = $day.'-'.$month;
I'm looking for a function that does the inverse of date(). Meaning:
$timestamp = inverse_date($format_string, $date_string);
$d = date($format_string, $timestamp);
if ($d === $date_string) {
echo 'This is what I want';
}
Problems I've run into so far:
strtotime - guesses format, so it might not be good for all formats
strptime - uses strftime's formatting that is different from date's
SOLUTION:
function inverse_date($format_string, $date_string) {
$dateTime = date_create_from_format($format_string, $date_string);
$value_ts = date_format($dateTime, 'U');
return $value_ts;
}
Thanks to Till Helge Helwig for the link.
function inverse_date($format_string, $date_string) {
$dateTime = date_create_from_format($format_string, $date_string);
$value_ts = date_format($dateTime, 'U');
return $value_ts;
}