Manipulating Dates in PHP without using Regular Expressions - php

Is there a way to change a date from
1985-12-15
to
1985-12
without using a regular Expression?

<?php echo date('Y-m', strtotime('1985-12-15')); ?>
That should do it.
This will, using strtotime, convert 1985-12-15 to a unix timestamp. The date function then takes a second parameter timestamp on which to format the date.

Convert the date to time via strtotime then use date to output in correct date format, like so:
<?php
echo date('Y-m', strtotime('1985-12-15'));

Maybe I'm just stupid, but if you only want the beginning of that date, stored as a string, can't you just use substr to extract the 7 characters at the beginning of that string ?
A bit like this, for instance :
$input = '1985-12-15';
$output = substr($input, 0, 7);
var_dump($output);
Which does give you :
string '1985-12' (length=7)
No need for any date-manipulation related function, in this case -- and this will probably be even faster/cheapier that parsing the string to a date and all that.
(Yeah, I know, premature optimisation ^^ )

$myDate = date('Y-m',strtotime('1985-12-15'));
echo $myDate // prints '1985-12'

If you think a regular expression isn't "cheap", then time functions will almost certainly be even more expensive: you would need to convert the string into a time value, then format it back into a string...

do you have that time value already in a array or do you whant such a like output come from the date function?

If you are not sure how the date will be formated, you should use the strtotime function, otherwise its probably as easy to do if the format is yyyy-mmm-dd or yy-m-d.
$datearray = explode('-',$date);
echo $datearray[0].'-'.$datearray[1];

not best to substr($date,0,strrpos($date,'-'));?
that would be cheapest?

Related

PHP convert string into date time

I have a php string from db it is 20/11/2017 I want to convert it milliseconds.
It's my code to doing that.
$the_date = "20/11/2017";
$mill_sec_date = strtotime($the_date);
var_dump($mill_sec_date);
But it does not print any thing rather than
bool(false);
What is the problem and how can i solve it ????
When using slashes to separate parts of the date, PHP recognizes the format as MM/DD/YYYY. Which makes your date invalid because there is no 20th month. If you want to use the format where day and month is swapped, you need to use hyphens, like DD-MM-YYYY.
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
print_r($newformat);
Use DateTime class to call function createFromFormat
$date = date_create_from_format('d/M/Y:H:i:s', $string);
$date->getTimestamp();
Most likely you got the date format wrong, see
here for a list of supported date and time formats:
This section describes all the different formats that the strtotime(), DateTime and date_create() parser understands.
You string is not accept by the strtotime, you can use createFromFormat set set the with the format type of the time string like below, you can also check the live demo. And you also can refer to this answer
var_dump(DateTime::createFromFormat('d/m/Y', "20/11/2017"));

PHP format date from returned data

I have a query that is returning a grid. One of the columns brings back a column with a date, like this:
echo "<td>{$Row[ETA]}</td>";
This displays the ETA from the database like this:
2013-10-30 20:00:0
I basically want to remove the TIME portion and just keep the date. Can this be done in the TD or do I have to the conversion elsewhere? I would like to just do the conversion within the cell, if possible.
Let me know how this can be done.
You can use the strtotime() and date() functions to achieve this!!!
date("Y-m-d", strtotime($Row[ETA]));
Well! if you want to get just date then you should use this function in your query
DATE(date_field)
as this will return only date from the datetime column
Ideally you should listen to the suggestion by JohnConde because it will limit your overhead between the database and your script but you can also substr() on the fly if you wish like this:
echo "<td>".substr($Row['ETA'], 0, 10)."</td>";
You have to echo it differently:
$eta = $Row['ETA'];
$etaDate = date("Y-m-d", strtotime($eta));
//now use $etaDate
You can use the date function to format the time, as what you're getting is a date as a string, you can use strtotime.
I think the format you're looking for is: date("Y-m-d", strtotime($Row["ETA"]));, you can either parse that into a variable and save it there, or you can concatenate the results together for the final string.

Convert or Extract Year, Month and Date from a string in PHP

All,
I have the following string:
$dateTime = '2013-09-15T00:00:00.000Z';
Is there a function to extract Year, Month and Date from the above string, so the result looks like the following:
$yearMonthDate = '2013-09-15';
Thanks
You could convert your datetime to a timestamp using strtotime() and then convert it back into a formatted date using this kind of syntax:
date("Y-m-d", strtotime($myOriginalDate))
substr or DateTime or strtotime+date
Since the first string is actually a standard, you can just use substr:
$yearMonthDate = substr($dateTime, 0, 10);
However, that would be kind of a hack and would obviously break if the format of $dateTime were to change. So, you might want to look into the PHP DateTime class instead.

formatting a date output

I've got an arry with dates.
print_r ($date[$i]);
will output sth. like:
2011-06-16
Is it possible to create an output like: 16.6.2011 ?
How would I do that?
have you got a reference?
<?
echo date('d.m.Y', strtotime( $date[$i] ) );
?>
you should find that this page has all the answers for you
http://php.net/manual/en/function.date.php
You can use the date function to print it, using the strtotime function to first convert it to a Unix timestamp. Or you can use the DateTime functions like:
date_create($date[$i])->format('d.n.Y');
If you don't want leading 0's on your day, then you should use:
date_create($date[$i])->format('j.n.Y');
use the php date() function
you can see a documentation about it here
http://php.net/manual/en/function.date.php
You don't really have a date, but a string. Either manipulate the string with the string manipulation functions, or parse and re-format the date string with the date functions.
date('d.n.Y', strtotime($date[$i]));
There is a reference when you search in Google for "date" ;)

PHP Time Problem

well, I"ve got a time format of the type: 2011-02-16T01:25:50+04:00
I want to strip the +04:00 from the end of the string. How to do it?
I am converting a timestamp from mysql to ISO 8601 format using this function:
$timeagotime = date('c',strtotime($row['created_at']));
where $timeagotime contains the value: 2011-02-16T01:25:50+04:00
Refer to the documentation for php's date function: http://php.net/manual/en/function.date.php
You need to use a different format string. For instance,
date('Y-m-d',strtotime($row['created_at']));
Givens you "2011-02-16". There are a number of format strings... check out the doc.
You could use substr() to remove it, or just generate a date without it in the first place, though you couldn't use the 'c' formatting option, since that'd just add the timezone on again.
$timeagotime = substr($timeagotime, 0, 19)
$timeagotime = date('Y-m-dTG:i:s', strtotime(...));
If you absolutely don't need the end then you can use the following. Otherwise I'd suggest using a different way to generate your date from the table. PHP Date
$timeagotime = substr(date('c',strtotime($row['created_at'])), 0, 19);

Categories