I'm not too good with dates in php (or any other language) and would need some help to convert a date's format into something more readable.
The string currently looks like this:
2021-03-31T00:00:00.0000000+02:00
But i would much rather prefer it to be similar to this when echo it:
2021-03-31 00:00:00
I have done some more or less useless stuff with string manipulation, but there must be a better way?
Example:
substr('2021-03-31T00:00:00.0000000+02:00',0,10);
How would one do this?
PHP has pretty extensive DateTime functionality that you can take advantage of, for this example you can use the DateTime class or DateTimeImmutable class.
$date = new DateTime('2021-03-31T00:00:00.0000000+02:00');
echo $date->format('Y-m-d H:i:s'); // 2021-03-31 00:00:00
You could also do it with string manipulation by
spliting string (e.g $datetime)with letter T to array by function explode
take first element and store it in a variable $date
take the second element of array and store it in $time (only 8 letters which represent time)
$datetime = '2021-03-31T00:00:00.0000000+02:00';
$date_time = explode('T', $datetime);
$date = $date_time[0];
$time = substr($date_time[1],0,8);
echo $date.' '.$time;//2021-03-31 00:00:00
Related
I am dealing with a problem of time conversion from 12 hr format to 24 hour format.
Is there any single function in php to replace the first two characters of a string?
str_replace can be used only when I know the substring content to be replaced.
$str_to_replace = '12';
$input_str = 'ab345678';
$output_str = $str_to_replace . substr($input_str, 2);
echo $output_str;
"12345678"
If the date is always given in a specific format you could try to convert it to a DateTime object and format the output.
$dateString = '15-Feb-2009 2:24 PM';
$date = DateTime::createFromFormat('j-M-Y g:i A', $dateString);
echo $date->format('Y-m-d G:i'); // will show "2009-02-15 14:24"
In general you should try in avoid holding a date in a string. Convert it to a DateTime -- this makes it also easier for you to manipulate the object (e.g. move date +1 day)
Assuming you have four numeric variables:
$Index=2; // 1-6, 2 is the second occurrence
$Dow=3; // 0-6, 3 is Wednesday
$Month=3; // 1-12, 3 is March
$Year=2016;
Is there a simple way to use those numbers to create or modify a DateTime object to represent that specific date? For example, with the variables above, it would be the specific date of the 2nd Wednesday in March of 2016.
I know that you can do this with strtotime or the DateTime constructor if you have it written out as a string already. There are other answers on here that deal with that.
But since we have numeric indexes, it seems stupid to create a function that converts numbers to a human readable string that then gets passed into a string parser to generate a date.
So the question is, is there a way to use strictly numeric variables with the DateTime object without having to first convert it to a human readable string to get a relative date like the 2nd(2) Wednesday(3) of March(3)?
I'm looking for something like:
$oDate = new DateTime();
$oDate->setFromWeekdayOfMonthIndex($Position,$DOWNum,$MonthNum,$Year);
UPDATE #1
Just to clarify the question a bit, this does what I want, but is a long/silly way of doing it:
$oMonthDate = new DateTime();
$oMonthDate->setDate($Year, $MonthNum, 1);
$oFormater = new NumberFormatter('en-US', NumberFormatter::SPELLOUT);
$oFormater->setTextAttribute(NumberFormatter::DEFAULT_RULESET,"%spellout-ordinal");
$String = $oFormater->format($Position).' ';
$String .= date('l', strtotime('Sunday +'.$DOW.' days')). ' of ';
$String .= $oMonthDate->format('F'). ' '.$Year;
$oFinalDate = new DateTime($String); // "second Tuesday of January 2016"
I have a datetime string which I would like to crop the 'time' part out of.
I need to do some calculation on it so I need to convert it to Unix time stamp.
What I've tried:
Use substr, and then strtotime, but when checking the result back to a human readable time format, it is not the same as the original date.
function convert($dbTime){
$createDate = new DateTime($dbTime);
$strip = $createDate->format('Y-m-d');
$yearMonthDateArray = explode("-", $strip);
}
The explode here crashes.
Edit:
This is the value of dbTime: "2014-07-27 12:06:00"
I want it to be "2014-07-27", and then have strototime on this format. This does not work. Converting it back to human readable date it generates 2014-07-06
Regarding comments:
I have tried all sorts of datetime functions. They either crash or they don't return the proper time
Explode crashes - it doesn't continue to the next line of code.
Edit2:
This is what's going on next. This returns false
$timeWithMakeTime = mktime(0,0,0,(int)$yearMonthDateArray[0], (int)$yearMonthDateArray[1],(int)$yearMonthDateArray[2]);
Discarding all your messy code, I assume you just want this:
$dbTime = '2014-07-27 12:06:00';
$date = new DateTime($dbTime);
$date->setTime(0, 0, 0);
echo $date->getTimestamp();
DateTime is an object, so what if you want to get timestamp you can do is as follows:
function convert($dbTime){
$createDate = new DateTime($dbTime);
return $createDate->getTimestamp();
}
You can read more about getTimestamp and other DateTime functions
Make sure your variable $dbTime is in correct format. For example DateTime does not support split seconds.
$date = date('H:i:s', strtotime($dbTime));
echo $date;
In PHP, I'm saving a date record as string in order to make conditions like "more than", "less than"; but I would really like to know how to decode it. What I want to do is something like this:
The moment that record is being saved is 2014, 08/06 1:30 in 24-hours format, so my integer should be like 201408060130, of course for this I use date() function.
But when it comes to decoding it to show it back like 2014, 08/06 1:30 or another format like 08/06 2014, 1:30 I really get stuck thinking on any solution for this.
I thought it would be like:
$date = date('YdmHm'); //Saving as 201408060130
$this->saveToDatabase($save, $mytable);
$dateDecoded = $this->getFromDatabase($mytable, $id, $theDateInteger);
$result = Date::getFormat($dateDecoded, 'YdmHm'); //Decode date format
echo $result->date('Y d/m, H:m'); //Show 2014 08/06, 1:30
This is simple & works. Just use strtotime:
$test_value = '201408060130';
echo date('Y d/m, H:i', strtotime($test_value));
The output is:
2014 06/08, 01:30
PHP has a really useful method under the Date class for this called DateTime::createFromFormat()
Here is an example usage:
$datetime = DateTime::createFromFormat('YdmHm', (string)$theDateInteger);
Now this is a valid datetime object which you can save to any formatting in a string if desired. For example:
echo $datetime->date('Y d/m, H:m');
I have a date in this format:
24-12-2010 // DAY - MONTH - YEAR
I need to get it in this format:
1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.
Check this link out:
http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
The above link is the way I need the date.
I am using PHP now, so this needs to be with PHP.
How can I convert these dates the easiest way?
Thanks
That is an ISO8601 format date; the following is what you want.
gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
You can do something like that:
$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");
The mentioned solution with:
$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);
does return strings like:
2012-11-28T17:21:11+0100
which cannot be parsed, at least with newer Solr versions.
I wouldn't use gmdate if you need to support timezones. The DateTime implementation is well done, and is also available for functional programming.
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/ref.datetime.php
You can use the DateTime class
$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);
$output = $dateTime.format(DateTime::W3C);
// Output now is your date in W3C format.
use the date ( string $format [, int $timestamp ] ) function of php!
In second paramter use http://php.net/manual/en/function.strtotime.php to get the timestamp from strings
$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);