Convert Date and Time to other Date and Time with PHP - php

I am getting date in format 2019-11-26T16:30:00+01:00 from an API and want to convert it to a format like Di. 26. Nov. 2019 / 16:30.
I am usinig following code, but always get 1 hour difference: Di. 26. Nov. 2019 / 15:30 Uhr.
If I use 2019-11-26T16:30:00+02:00 - I get Di. 26. Nov. 2019 / 14:30 Uhr (2 hours earlier).
Here is my php code:
$timstp = strtotime('2019-11-26T16:30:00+1:00');
echo date('d.m.Y H:i:s', $timstp);
How can I get correct date?

Use the DateTime class. The format method returns the result in English.
$input = '2019-11-26T16:30:00+1:00';
$date = date_create($input)->format('D. d. M. Y /H:i \U\h\r');
echo $date; //Tue. 26. Nov. 2019 /16:30 Uhr
For an output in other languages ​​like German I recommend the DateTime extension class dt.
$input = '2019-11-26T16:30:00+1:00';
$date = dt::create($input)->formatL('D d. M Y / H:i \U\h\r','de_DE');
echo $date; //Di. 26. Nov. 2019 / 16:30 Uhr
Update:
Does the API get entries from different time zones? If so, is the question what is needed?
The local time of the time zone or a unique time base for comparability?
The examples above show the local time of the time zone.
To create a uniquet basis, the DateTime object can be converted to a different time zone how UTC.
$input = '2019-11-26T15:30:00+5:00';
$date = date_create($input)
->setTimeZone(new DateTimeZone('UTC'))
->format('D. d. M. Y / H:i:s')
;
echo $date; //Tue. 26. Nov. 2019 / 10:30:00

try this
$datetime = new DateTime('2019-11-26T16:30:00+1:00');
echo $datetime->format('d.m.Y H:i:s');
find more details about DateTime here

Related

Get Unix timestamp of specific date after another specific time

I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();

php date addition skipping days

I have a date, and need to add 24 Months (and not 2 Year) to it. This is what I tried:
strtotime("+24 months", $mydate);
If my date is, 20th Dec 2013 then the computed date is coming as 10th Dec 2015, whereas my expected date was 20th Dec 2015.
I know, what is going behind the scene:
2 Year: 365 days x 2 = 730 days
24 Months: 24 x 30days = 720 days
This gives me the missing 10 days. But how to over come this issue.
In Java we have Calendar class, which takes care of such calculations. However, I din't find anything here.
Can this issue be resolved.? Or I need to handle it manually?
You should always use the DateTime() class for anything like that.
i.e
$date = new DateTime("UTC");
//get date in 24months time:
$date->add(new DateInterval("P24M"));
//output date:
echo $date->format("d/m/Y H:i:s");
By using the DateTime and DateInterval classes, you can be sure that it will account of leap years and other such irregularities in dates.
See more at: http://php.net/manual/en/class.datetime.php
I hope this helps.
$mydate = "2014-10-01";
echo date('d-m-Y',strtotime("+24 months", strtotime($mydate)));
DateTime should work perfectly here:
$date = new DateTime("20th Dec 2013");
echo $date->format("d-m-Y");
$date->add(new DateInterval('P24M'));
echo $date->format("d-m-Y");
Output:
20-12-2013
20-12-2015
Demo
Sidenote:
I couldn't reproduce your error with:
echo date("d-m-Y", strtotime("+24 months", $mydate = strtotime("2013-12-20")));
See:
http://3v4l.org/HURAt

Get current datetime using PHP

I want to add content to a MySQL table with current date and time.
When I insert the content to database it shows the correct date but the wrong time.
$date1 = date("Y-m-d H:i:s");
date_default_timezone_set('Asia/Kolkata');
$date1 = date("Y-m-d H:i:s");
Asia/calcutta has changed to Asia/Kolkata
[a] strftime(): Format a local time/date according to locale settings
[b] date : Format a local time/date
Examples
Consider following simple example:
<?php
print strftime('%c');
?>
Output:
Mon Apr 23 01:22:58 2007
You need to pass format such as %c to strftime() to print date and time representation for the current system. You can use following format characters:
%m - month as a decimal number (range 01 to 12)
%d - day of the month as a decimal number (range 01 to 31)
%Y - year as a decimal number including the century
You can see the complete format conversion specifiers online here
You can also use date() as follows:
<?php
print date('r');
print "\\n";
print date('D, d M Y H:i:s T');
print "\\n";
?>
Output:
Mon, 23 Apr 2007 01:29:56 +0530
Mon, 23 Apr 2007 01:35:14 IST

how to php convert this time format?

i was fetching this date from table in the database like this format
Sunday 16th of January 2011 06:55:41 PM
and i want to convert it to be like this format
11-05-2012
how to do that with date function or any function
when i use date function
<td><?php echo date('d-m-Y', $v['v_created']); ?></td>
i get error message
'Severity: Warning
Message: date() expects parameter 2 to be long, string given'
This works for me (just tested on local web server)
<?php
date_default_timezone_set ('Europe/Rome');
$date = "Sunday 16th of January 2011 06:55:41 PM";
//.Strip "of" messing with php strtotime
$date = str_replace('of', '', $date);
$sql_friendly_date = date('y-m-d H:i', strtotime($date));
echo $sql_friendly_date;
?>
You can format the date as you prefer changing the first parameter of Date function according to: http://it2.php.net/manual/en/function.date.php
You have the following format:
Sunday 16th of January 2011 06:55:41 PM
that is a string based format, so the date information is more or less encoded in a human readable format. Luckily in english language. Let's see, that are multiple things all separated by a space:
Sunday - Weekdayname
16th - Date of Month, numeric, st/nd/th form
of - The string "of".
January - Monthname
2011 - Year, 4 digits
06:55:41 - Hour 2 digits 12 hours; Colon; Minute 2 digits; Colon; Seconds 2 digits
PM - AM/PM
So you could separate each node by space and then analyze the data. All you need is all Monthnames and the sscanf function because you only need to have the month, date of month and year:
$input = 'Sunday 16th of January 2011 06:55:41 PM';
$r = sscanf($input, "%*s %d%*s of %s %d", $day, $monthname, $year);
Which will already give you the following variables:
$monthname - string(7) "January"
$day - int(16)
$year - int(2011)
So all left to do is to transpose the monthname to a number which can be done with a map (in the form of an array in PHP) and some formatted output:
$monthnames = array(
'January' => 1,
# ...
);
printf("%02d-%02d-%04d", $day, $monthnames[$monthname], $year);
So regardless of which problem, as long as the input is somewhat consistently formatted you can pull it apart, process the gained data and do the output according to your needs. That is how it works.
try this. always worked for me
$date = Sunday 16th of January 2011 06:55:41 PM
$new_date = date('d-M-Y', strtotime($date));
echo $new_date;
The format you are using Sunday 16th of January 2011 06:55:41 PM is a wrong format.from the form you are inserted this date in database should be in date(Y-m-d) than the value of date inserted in database like:- 11-05-2012. and you can fetch this and get the format what you want.
<?php
$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$new_date = date('d-M-Y', strtotime($old_date));
echo $new_date
?>
more details about date plz visit this url
http://www.php.net/manual/en/datetime.createfromformat.php

Using PHP to insert/output dates and time in mysql?

I don't know why it is so confusing, maybe it s because there are so many ways to output/input date and time with mysql and php. All I want is for users to put a date in this format
MM/DD/YYYY
and for it to output in this format
Month Name - Date
and for time, I want users to pick the time from a select field, starting with 1:00 P.M. and incrementing down 30 minutes, so it will be 1:30 P.M., 2:00 P.M... and I want to somehow insert this into MySQL and output it as 1:30 P.M. Not sure how to do that either.
The best way to store a date format in a database and be able to display it the way you want is to use the time() and date() function.
when storing the date in your database you should use time(), it will generate a string like this -> 1300695900. this little string contains the date and time
then you can use date to display it in any way you want:
$time = time();//you would normally get this from the database
date('d M Y', $time); //outputs 21 Mar 2011
date('m-d-y', $time); // outpus 03-21-2011
and so on...
Edit:
to answer your last question, you just get the different values, stick it together (concatenation) and then use strtotime()
$date = $_POST['date']; // eg 03/03/2011
$time = $_POST['time']; // eg 1:30
$daypart = $_POST['daypart']; // eg PM
$date_time = $date.' '.$time.' '.$daypart;
$strtime = strtotime($date_time);
echo date('d M Y - h:i', $strtime);// outputs 03 Mar 2011 - 01:30

Categories