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);
Related
I am getting a date from the mysql database:
here is what it comes out as:
2017-01-20
what would be the fastest way to get the month, day, and year, so for example, when i echo, it will be like this:
echo $month; //01
echo $day; //20
echo $year; //2017
Well, if you know that the output will consistently be a string in the format "YYYY-MM-DD", the most basic approach is:
<?php
$query = ... //query is your string "YYYY-MM-DD"
$year = substr($query, 0, 4);
$month = substr($query, 5, 2);
$day = substr($query, 8, 2);
echo $month;
echo $day;
echo $year;
?>
Let's assume you have that string in a variable called $date
$date = '2017-01-20';
You can explode it into a list if you are sure the format is consistent:
list($year, $month, $day) = explode("-", $date, 3);
You could convert the date to a time integer using strtotime to use in other functions like date. This has the added benefit of being able to test that this is a well-formed date:
$time = strtotime($date);
if ($time === false) die("Bad date format: $date.");
$year = date('Y', $time);
$month = date('m', $time); // 'n' if you don't want leading zero
$day = date('d', $time); // 'j' if you don't want leading zero
As jasonmoqio points out, since you asked for fastest, substr is a tiny bit faster than exploding. (On my workstation looping substr vs. explode 10 million times only produced an improvement of 1/1000th of a second over exploding, so unless this is in a loop that gets run millions of times, you will not notice the difference and should opt for code readability.)
$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
Try this:
$date = new DateTime('2017-01-20');
echo 'Year:'.$date->format("Y");
echo 'Month:'.$date->format("m");
echo 'Day:'.$date->format("d");
Output:
Year: 2017
Month: 01
Day: 20
If you want to quickly get the date from mysql, try using regex like this.
if (preg_match('/^(?P<year>\d+)[-\/](?P<month>\d+)[-\/](?P<day>\d+)$/', $your_date, $matches)) {
$mydate = $matches['year'] . "-" . $matches['month'] . "-" . $matches['day'];
$whatever = date('Y-m-d', strtotime($tgl));
// You can echo it...
// echo $matches['year'];
// echo $matches['month'];
// echo $matches['day'];
}
Hope this help you out. :D
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.
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.
I have a string in the format yW. Year followed by the week number. I would like to get a timestamp. I am using Php 5.2.17 on Windows.
strtotime() does not seem to be working reliably. For the string '1142' it should return the first day of the 42nd week of 2011.
Any suggestions on how to do this?
Give this a go, will work on windows also...
$date = '1201';
$y = substr($date, 0, 2) + 2000;
$w = substr($date, 2);
$ts = mktime(0, 0, 0, 0, 0, $y) + ($w * 604800);
subtract 1 from week if week 1 is the first week of the year
strtotime() does not accept that particular format. However, it can work with year and week number values if they're formatted as 2011W42.
// Convert 1142 to 2011W42
$reformatted = '20'.substr_replace('1142', 'W', 2, 0);
$timestamp = strtotime($reformatted);
See Compound Formats for details on this particular format.
Another option is the setIDODate() method on the DateTime class.
sscanf('1142', '%2d%2d', $year, $week);
$date = new DateTime('#0');
$date->setISODate(2000 + $year, $week);
$timestamp = $date->format('U');
I'm trying to a date timestampted in my mysql db, but my code shows the current date instead.
This is the code that I'm trying:
$db_date = $row_news['nm_date'];
$year = substr($db_date, 0, 4);
$mon = substr($db_date, 4, 2);
$day = substr($db_date, 6, 2);
$orgdate = date("l dS F Y",mktime($mon, $day, $year));
$date = $orgdate
The script is meant to email $date, which should be the value of $row_news['nm_date'], but instead I get the current date "Saturday 11th September 2010.
Thanks for your help.
You're getting today's date because mktime() is not returning a valid value. If you check the manual entry for mktime() - http://www.php.net/mktime - you'll see that the parameter order is:
mktime ($hour, $minute, $second, $month, $day, $year)
so you probably want:
$orgdate = date("l dS F Y", mktime(12, 0, 0, $mon, $day, $year));
your code assumes that the date is in YYYYMMDD format (or YYYYMMDDHHIISS). Assuming that is correct, and it's not actually in date format (YYYY-MM-DD) then the above should fix your problem.
Edit: If the dates are in YYYY-MM-DD format you need to adjust your substrings to allow for the dashes:
$year = substr($db_date, 0, 4);
$mon = substr($db_date, 5, 2);
$day = substr($db_date, 8, 2);
$db_date = $row_news['nm_date'];
$orgdate = date("l dS F Y", $db_date);
$date = $orgdate;