strtotime conversion - php

How do I format this str coming from a database to something more readable.
2012-04-06T10:55:58-07:00
I would like it in the format of m-y.
I have tried
$date = date('m-y',strtotime('2012-04-06T10:55:58-07:00'));
I am stumped.
Thanks!

If the time zone isn't meaningful you can chop it off like this:
$dbDateString = '2012-04-26T10:55:58-07:00';
$dateString = substr($dbDateString, 0, 10);
$date = date('m-y',strtotime($dateString);
or you can go the cheap route:
$dbDateString = '2012-04-26T10:55:58-07:00';
$year = substr($dbDateString, 0, 4);
$month = substr($dbDateString, 5, 2);

Check out: How do I convert an ISO8601 date to another format in PHP?
They solution they provided was:
$date = '2011-09-02T18:00:00';
$time = strtotime($date);
$fixed = date('l, F jS Y \at g:ia', $time); // 'a' is escaped as it's a format char.

Related

how to do general conversion of date from different formats (some times with extra string) to Y-m-d format

In my CodeIgniter application, I am getting date in different formats, such as: April 1st 2017, May 29, 2015, Jun-15-2015, 10-September-2015 and sometimes even with extra string such as Start: April 1, 2017. However, I want to convert the input date from any format to Y-m-d in order to save it in MySQL database. For example, if input date is April 1st 2017 I should get 2017-04-01. I have used below posted code for that but it is not working for all of the above mentioned cases. So please tell how can I write general conversion logic that can convert date from any format even if date has extra string with it (as mentioned above) to Y-m-d format.
Code:
$date = DateTime::createFromFormat('F jS Y', $old_date);
$new_date = $date->format('Y-m-d');
try this
$old_date = 'Jun-15-2015';
echo $newDate = date("Y-m-d", strtotime($old_date));
You can replace the extra string like
$date = str_replace('Start: ',' ',$date);
And after you can use date function of php
echo $date = date('Y-m-d',strtotime($date));
This might help even for inserting this date format into database tables
function convertUTCCombinedToLocal($utcDateTime) {
$utcDateTime = explode(" ", $utcDateTime);
$date = explode("-", $utcDateTime[0]);
$time = explode(":", $utcDateTime[1]);
$localDate = new DateTime();
$localDate->setTimezone(new DateTimeZone('UTC'));
$localDate->setDate($date[0], $date[1], $date[2]);
if (count($time) == 3) {
$localDate->setTime($time[0], $time[1], $time[2]);
} else {
$localDate->setTime($time[0], $time[1], '00');
}
$localDate->setTimezone(new DateTimeZone('Asia/Kolkata'));
return $localDate->format('d/m/Y H:i:s');
}

PHP get the year from the variable

I usually mess this up, but I am trying to get a year from a variable $date = 2011-01-01 I am trying to get 2011 from the variable...I tried this but it didnt echo anything out...
date(strtotime("Y"), strtotime($date));
$year = date('Y', strtotime('2011-01-01'));
Or, if you don't want to convert to date, and trust your input format:
$year = explode('-', '2011-01-01')[0];
The above requires PHP 5.4, on older versions you need two lines:
$arr = explode('-', '2011-01-01');
$year = $arr[0];
Or yet, this slightly faster alternative suggested by tigrang:
$year = substr($date, 0, 4);
Or, an even faster way, as salathe suggested:
$year = (int) $date;
(everything after the first dash will be ignored, so $year will contain an integer with the year part of the date (unlike the other options, where $year would be a string).
date('Y', strtotime($date)) should do
First arg is the format, second is timestamp.
You can explode the date into a list and call each date item individually.
$date = '2012-01-01';
list($year, $month, $day) = explode('-', $date);
echo $year; // echoes 2012
May I suggest using PHP's DateTime class as an alternative:
$date = '2011-01-01';
$dateTime = new DateTime($date);
echo $dateTime->format('Y');
DateTime is all well and good, but lets go old school.
sscanf($date, '%d', $year);
// or
$year = strtok($date, '-');
// or
$year = substr($date, 0, 4);
// or
$year = (int) $date;
or, putting the sensible hat back on, you could use DateTime or correctly use date().
$datetime = new DateTime($date);
$year = $datetime->format('Y');
// or
$year = date('Y', strtotime($date));

convert a date in this format '031012' into a date that I can add days to

I have a date in this format 030512 (ddmmyy).
But I'm having trouble with converting this to a date usable format I can add days to.
Basically.. I extracted the date above from a text file, Now I need to be able to add a number of days to it. But I am having trouble parsing the date in this format.
Is there another way of doing this rather then something like this:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
$date_after = $day . "-" . $month . "-".$year;
// Now i need to add x days to this
$total_no_nights = 010; // must be this format
$days_to_add = ltrim($total_no_nights,"0"); // 10, remove leading zero
// how do i add 10 days to this date.
You can do this (php >= 5.3):
$date = DateTime::createFromFormat('dmy', '030512');
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://www.php.net/manual/en/datetime.createfromformat.php
For php < 5.3 :
$dateArray = str_split('030512', 2);
$dateArray[2] += 2000;
echo date("d/m/Y", strtotime('+1 day', strtotime(implode('-', array_reverse($dateArray)))));
try this using the month/day/year you already have:
$date = "$month/$day/$year";
$change = '+10 day';
echo date("d/m/Y", strtotime($change, strtotime($date)));
Assuming the date will always be in the future (or at least after 1st Jan 2000), you're not far wrong:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
// dd-mm-yy is not a universal format but we can use mktime which also gives us a timestamp to use for manipulation
$date_after = mktime( 0, 0, 0, $month, $day, $year );
// Now i need to add x days to this
$total_no_nights = "010"; // must be this format
$days_to_add = intval( $total_no_nights ); // No need to use ltrim
// Here's the "magic". Again it returns a timestamp
$new_date = strtotime( "+$days_to_add days", $date_after );
Using the DateTime object would be easier but you say you're not on PHP5.3.
You can't do date manipulation with strings becase, well, they are not dates. In PHP, you can use Unix timestamps (which are actually integers...) or DateTime objects. In you case:
$timestamp = strtotime('-10 days', mktime(0, 0, 0, $month, $day, $year));
echo date('r', $timestamp);
... or:
$object = new DateTime("$year-$month-$day");
$object->modify('-10 days');
echo $object->format('r');
Also, please note that 010 is an octal number that corresponds to 8 in decimal.
using the convert function in sql the date can be obtained in appropriate format.
anter that operations can be performed in php to manipulate the date. i hope that answers your query.

How to convert the date into paticular format using PHP?

I am having the input box for getting the value in mm-dd-yyyy format.So When I get the date I try to covert it into YYYY-MM-DD format.But It doesnt work.
For Example :
<?
$birthdate="08-13-2000";
$date=date('Y-m-d',strtotime($birthdate));
echo $date;
?>.
Output is 1970-01-01.
But If I gave 13-08-2000 I got 2000-08-13.I dont want to split.Because in my application I used in manyplaces like this.But I think the strtotime convert it into unix timestamp format even whatever the value.That's why I am try to do like.What's wrong I am understanding or doing?Thanks in advance.
strtotime() parses dash-separated dates as dd-mm-yyyy. You'll need to input birthdate as "08/13/2000". str_replace should do the job for that if you can't change the expected seperator for the input.
credit for the separator differences to sam at frontiermedia dot net dot au from php.net
Edit: Have some sample code for if you need to do the replace:
$birthdate = '08-13-2000';
$birthdate = str_replace('-','/',$birthdate);
$date = date('Y-m-d',strtotime($birthdate));
echo $date;
Otherwise it'd just be
$birthdate = '08/13/2000';
... snip ...
Try this:
$date = explode("-", "08-13-2000"); // input MM-DD-YYYY
$time = mktime(0, 0, 0, $date[0], $date[1], $date[2]); // to time
$date = date('Y-m-d', $time); // transform to Y-m-d
echo $date; // output YYYY-MM-DD
$date[0] is the month
$date[1] is the day
$date[2] is the year
And if you use it in manyplaces, make a function():
function transformDate($input)
{
$date = explode("-", $input);
$time = mktime(0, 0, 0, $date[0], $date[1], $date[2]);
$date = date('Y-m-d', $time);
return $date;
}
echo transformDate("08-13-2000");

php - help converting timestamp string to readable time format

I have a record called Time with the following date string: 20100902040003 in the input file.
I need some php help to convert this to something more readable such as this format: 2010-09-02 04:00:03
and would like to format it as I print out the table data.
$timestamp = "20100902040003";
echo date('Y-m-d H:i:s', strtotime($timestamp)); // output: 2010-09-02 04:00:03
If you're sure that all records in your input file have the format YYYYMMDDHHmmss, you could try to split the string yourself and then using date() in conjunction with mktime() to generate a meaningful date format.
Since your data is already in the right format, it just needs some punctuation, so you can use a regular expression, rather than the date-parsing and date-formatting functions.
$timestamp = "20100902040003";
preg_replace('/(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/',
'\1-\2-\3 \4:\5:\6', $timestamp)
yields:
2010-09-02 04:00:03
Since the timestamp is not a unix timestamp, you have to use substr()
$timestamp = "20100902040003";
$year = substr($timestamp, 0, 3);
$month = substr($timestamp, 4, 5);
$day = substr($timestamp, 6, 7);
$hour = substr($timestamp, 8, 9);
$minute = substr($timestamp, 10, 11);
$second = substr($timestamp, 12, 13);
Then you arrange that using sprintf()
$formatted_timestamp = sprintf('%s-%s-%s %s:%s:%s', $year, $month, $day, $hour, $minute, $second);

Categories