PHP date and time from an xml import - php

I have an xml file where the date stored in this format:
<sun rise="2014-05-30T02:51:30" set="2014-05-30T18:31:22"/>
My php process this value to a variable named $sunrise
2014-05-30T02:51:30
But my timezone is +2 so I have to add 2 more hours.
The only problem with this its just a string.
I dont have any idea to how to convert it to date.

Since you already got the time inside that element (2014-05-30T02:51:30) you could just use strtotime() or alternatively, you could also use DateTime + DateInterval to add 2 more hours. Consider this example:
$sunrise = '2014-05-30T02:51:30';
$date = new DateTime($sunrise);
$date->add(new DateInterval('PT2H'));
echo $date->format('Y-m-d H:i:s');
// outputs: 2014-05-30 04:51:30
echo date('Y-m-d H:i:s', strtotime($sunrise . ' +2 hours'));
// outputs: 2014-05-30 04:51:30

Related

Convert Now and Ten Minutes Ago to Different Timezone

I would like to convert the timestamps from ten minutes ago and now to match the following format:
2018-09-23T04:47:07.237
Here are the timestamps I'd like to convert to match the above format:
$now = date('m/d/y g:i a');
$now = strtotime($now);
$ten_minutes_ago = strtotime('-10 minutes');
How can I do this? Thanks!
Use date_format function instead. You don't need to convert to UNIX timestamp using strtotime function. Instead use DateTime library
Check the following (Rextester Demo):
$now = new DateTime(); // create a datetime object
$sub = new DateInterval("PT10M"); // Interval of 10 mins
$ten_minutes_ago = new DateTime();
$ten_minutes_ago->sub($sub); // subtract 10 minutes
// changed formats
$now_changed = date_format($now, DATE_ISO8601);
$ten_minutes_ago_changed = date_format($ten_minutes_ago, DATE_ISO8601);
// print output
echo $now_changed; //2018-09-23T02:58:25-0400
echo $ten_minutes_ago_changed; // 2018-09-23T02:48:25-0400
Details:
The date_format() function returns a date formatted according to the specified format.
DATE_ISO8601 - ISO-8601 (example: 2013-04-12T15:52:01+0000)
You can check for more formatting options here.
Here is how I would do what you are asking for.
If your data is in a string. Here is the only line of code you need:
$date = date('m/d/y g:i a'); //Gets a date string.
echo substr(date('Y-m-d\TH:i:s.u', strtotime($date . ' -10 minutes')), 0, -3); // PHP < 7.0.0
//echo date('Y-m-d\TH:i:s.v', strtotime($date . ' -10 minutes')); //PHP > 7.0.0
This will produce:
Ex.
09/23/18 12:13 am
To
2018-09-23T00:03:00.000
One thing to note here. The microseconds will always be zeros if your original input date is a string and in the format m/d/y g:i a that you have specified. The reason being is that there is no millisecond information to be had from the date string.
If you create you input date as a dateTime object, the object will be able to keep track of the microseconds for you.

adding one day to a custom formatted date string

I have a string in the format YYYYMMDDHH24MISS that is year, month, day, hours, minutes, seconds. I want to convert this to a date, add one day to it and return it in the same format. Sounds simple but I am unable to get this to work. I have tried a number of different ways where $field3 contains the date string for example:
$end_date = strtotime(substr($field3,1,8));
$date_interval = DateInterval::createFromDateString('1 day');
$new_end_date = date_add($end_date, $date_interval);
$field3 = ($new_end_date->format('YYYYMMDD')).substr($field3,8,6);
In this example $new_end_date contains "false".
Example date time string: 20170912124159 being 12/09/2017 12:41:59
The format of your input string can be parsed by the constructor of class DateTime (and date_create() and strtotime()) without problems.
$date = new DateTime('20170912124159');
$date->add(new DateInterval('P1D'));
echo($date->format('Y-m-d H:i:s'));
# The output is:
# 2017-09-13 12:41:59
You can, as well, format the date as string using the format YmdHis to get the modified date in the same format as the input string.
echo($date->format('YmdHis'));
# 20170913124159
Read about DateTime and DateInterval.
You can try something like this:
$date = DateTime::createFromFormat('YmdHis', '20170912131313');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d');
For more information: http://php.net/manual/en/datetime.add.php
Please try this
$date = date("Y/m/d H:i:s"); // or '2017/09/30 20:24:00'
$ndate = date('Y/m/d H:i:s', strtotime($date . ' +1 day'));
echo 'date after adding 1 day: ' . $ndate;

Formatting Duration From String in PHP

i want to calculate different dates from sting, string format is (01:50:30),
For Example:
1: (01:35:00)
2: (04:45:50)
3: (02:35:36)
4: (01:00:00)
5: (06:35:47)
How to calculate these stings with date format?
You can use DateTime
<?php
date_default_timezone_set('America/Los_Angeles');
$time = DateTime::createFromFormat('H:i:s', '12:05:59');
var_dump($time);
$time->modify('5 minutes');
$time->modify('1 hour');
$time->modify('-30 seconds');
var_dump($time);
You won't be able to work out the date solely from the times you have provided, you can turn it into a DateTime object which will set the current date to today and used the times you provided as the time.
For example:
$time = '(01:35:00)';
$date = DateTime::createFromFormat('(' . 'H:i:s' . ')', $time);
echo $date->format('Y-m-d H:i:s');
Will output:
2016-01-25 01:35:00
You can then easily manipulate the DateTime object as you need. The full documentation can be found in the PHP manual:
http://php.net/manual/en/class.datetime.php

Date format PHP

I am trying to format a date as 2015-07-12 15:00 from the values declared in my variables
// unix
$date = 1436713200
// string
$time = '15:00';
to get a date format 2015-07-12 15:00 but failing, using this
$newdate = date('Y-m-d H:i:s', $date.' '.$time);
I get 'A non well formed numeric value encountered'. Can anyone help? I understand it is possibly due to the mix of string and unix but unsure how to get round this.
I would suggest you to use DateTime instance to avoid timezone issues:
$d = date_create('#1436713200'); // creates DateTime instance
$d->setTime(15, 00); // sets current time to desired hours, minutes
echo $d->format('Y-m-d H:i:s'); // prints it out with format specified
//⇒ 2015-07-12 15:00:00
You do not have to provie the $time variable. Unix time is a full date with time.
Use:
$newdate = date('Y-m-d H:i:s', $date);
Use this
$date = date('Y-m-d','1436713200');
// string
$time = '15:00';
echo $newdate = date('Y-m-d H:i', strtotime($date.' '.$time));

How to add months to a particular date

I just hope this question won't be marked as a duplicate because I've seen similar questions on stackoverflow but they all talk about adding days to the date, The problem here is that i want to add some particular months to a particular date which is gotten from my database I've tried adding it using strtotime() but the date just returns 1st January 1970, the code looks like this
<?php echo date('jS F Y', strtotime("$date +1 month")); ?>
//This is the value of date
$date = $student->date;
How to I add months to this particular date? Please note that the date is a timestamp in my database.Thanks
You have a Unix timestamp, not an actual date. Here I use the DateTime class to create a datetime object using that Unix timestamp. Then I can add a month to it and format the output.
$date = new DateTime('#'.$student->date);
$date->modify('+1 month');
echo $date-format('jS F Y');
If you want to stick to using date() and strtotime() you would use this:
echo date("jS F Y", strtotime("+1 month", $student->date));
strtotime() would take the starting date as the second parameter and then how you wish to modify it as your first parameter.
You should check out the documentation here,
But the just of it is the $date->add function. It allows you to add any amount of time to a timestamp using a DateInterval. Its a little tricky to get used to but here are a couple of examples:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
which outputs:
2000-01-01 10:00:30
2007-06-05 04:03:02
The date interval is formatted in years months days hours minuets seconds, simply put in the amount you want and it will add it, so in your case:
<?php
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
PHP's strtotime() function allows for a second parameter that allows you to set a relative date.
If you would like to add a month to tomorrow, here's how:
<?php
echo date("jS F Y", strtotime("+1 month", strtotime("2014-10-09")));
// returns: 9th November 2014

Categories