facing issue in strtotime() [duplicate] - php

This question already has an answer here:
Date in a URL dd/mm/yyyy
(1 answer)
Closed 5 years ago.
I am facing an issue in echo date() with some formats like:
if i use the format d/m/Y like: '18/04/2017' then date() does not recognize this because the format contain /
<?php
echo date("d-m-Y",strtotime('16/04/2017'));
?>
it will output: 01-01-1970
But when i use the format m/d/Y like: '04/18/2017' then date() it recognized
<?php
echo date("d-m-Y",strtotime('04/16/2017'));
?>
it will output: 16-04-2017
I am not getting these different behaviour of date(), can anybody help me please

Forward slash (/) signifies American M/D/Y formatting, a dash (-)
signifies European D-M-Y and a period (.) signifies ISO Y.M.D.
So if your date is like 04/16/2017, change the / to - and then use date() function on it to convert it to any format.
Reference
Working Code

You can use date_create_from_format to convert your date to any format you want. The problem with your code was that strtotime does not support this format.
Learn about strtotime formats
If you using dd/mm/yyyy then it will not work because does not comes under support format of strtotime. For using
Supported formats: (for dd mm yyyy)
dd [.\t-] mm [.-] YY
Example: 16-04-2017
Example: 16.04.2017
Example: 16\t04t\2017
<?php
ini_set('display_errors', 1);
$date=date_create_from_format("d/m/Y","16/04/2017");
echo $date->format("d-m-Y");

Another way is replace / with - of given date
$date = '16/04/2017';
$date = str_replace("/", "-", $date);
echo date("d-m-Y",strtotime($date));

Related

Converting string of numbers to date in PHP [duplicate]

This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
I'm trying to convert a string of numbers I am pulling from a db that looks like this '010219', representing January 2, 2019. I cannot find a way to convert this into 2019-01-02 using php, I just keep getting today's date from the functions I am trying.
Needs to be accomplished with no separators in original string.
There are a variety of ways to accomplish this, however the most concise is probably to use date_create_from_format.
An example is here:
$date = date_create_from_format('dmy', '010219');
This will output a Date as so:
echo date_format($date, 'Y-m-d');
Outputs: 2019-02-01
The date_create_from_format function accepts a parameter that defines the format of the date. In this case, the format is dmy which means:
d - Day of month as two-digit number (01-31)
m - Month of year as two-digit number (01-12)
y - Year as two-digit number
The documentation for date_create_from_format is here.
have you tried something like this?
<?php
$str="010219";
$date = DateTime::createFromFormat('dmy', $str);
echo $date->format('Y-m-d'); //2019-02-01
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2003-10-16
Please see source: Converting string to Date and DateTime
split and concatenate with preg_replace
$newformat = preg_replace("/^(\d\d)(\d\d)(\d\d)$/","20$3-$1-$2","010219");

How to add day to a string in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
My current Date format is in MM/YY. I Need to default days to my format in php.
For example:
12/2009 -> 07/12/2009
I tried this code:
$currdate = '07/'.$currdate;
$newFormat = date('d-M-Y',strtotime($currdate));
But the new format is wrong, it output 12/07/2009.
----------------- Edit -----------------------------
I have tried **DateTime::createFromFormat**.Since my $currdate date format has only month and year its not accepting.I am getting a fatal error.
strtotime expects an american date format. Use datetime::createFromFormat instead:
$date = DateTime::createFromFormat('d/m/Y', $currdate);
echo $date->format('Y-m-d');
Edited to better explanation:
When you use date with slashes(/), PHP strtotime will think it is in m/d/Y format, the american way.
If you use dash (-) it will assume d-m-Y format.
If you use dot (.) it will assume Y.m.d format.

Wrong date while converting date in php

I used the following code to convert date in php, but I am getting wrong date after the conversion. I used CURRENT_TIMESTAMP as default value in my database table.I am passing "date" variable from javascript.
Can anyone please solve this problem. Thanks in advance.
$date1=mysql_real_escape_string($data->date);
print_r($date1) // This is one displaying the right date.
$date=strtotime($date1);
$timedatenew=date('Y-m-d H:i:s', $date);
print_r($timedatenew) // This is displaying the date like "1970-01-01 01:00:00".
As u said u have date in format dd/mm/yy try with this
$timedatenew= date("Y-m-d H:i:s",strtotime(str_replace('/', '-', $date ))));
it convert / to -
You are getting wrong date because when you use date('Y-m-d',strtotime($date)) for converting date format then Dates in the m/d/Y or d-m-Y formats are disambiguated by looking at the separator between the various components:
if the separator is a slash (/), then the American m/d/Y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-Y format is assumed. Check detail here: php.net/manual/en/function.strtotime.php
(above part is commented by hjpotter92 in same this type of other question)
if you want exact right format then use DateTime object, it can format the date from any string format, it also works well with other formatter other than - like / or .
that means you can change date format from:
d/m/Y or d-m-Y or d.m.Y to your any desired format like Y-m-d.
$date = DateTime::createFromFormat('d/m/Y', "12/07/2019");
echo $date->format('Y-m-d');
// output: 2019-07-12
see below examples from the php official doc DateTime::createFromFormat
Example DateTime::createFromFormat()
Object oriented style
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
Procedural style
<?php
$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');
?>
The above examples will output:
2009-02-15

Converting between date formats in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 1 year ago.
I have a Web for that requires the user to enter a date which is then stored in a MySQL database. I'd like to have the user enter the date in m/d/yyyy and have the system convert it into the Y-m-d format that MySQL requires. I thought that was simple enough, but I can't get it to stop making a serious error,
I've tried the following:
$date = new DateTime($this->vital_date);
$this->vital_date = $date->format('Y-m-d');
When the user enter 9/6/2013, 2013-06-09 gets stored in the MySQL table. (Note the transposition of the month and date.
Then, I tried the older, pre-object way:
$date = strtotime($this->vital_date);
$this->vital_date = date('Y-m-d', $date);
And that did the same thing -- a transposed month and date.
Can anyone give me any help on what I'm doing wrong or how I could make a better conversion.
You should be using DateTime::createFromFormat
From PHP DOC
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Example
$vital_date = "9/6/2013";
$date = DateTime::createFromFormat("m/d/Y", $vital_date);
echo $date->format('Y-m-d');
You should use DateTime::createFromFormat for non-standard format
You can specify the format you are giving the data in the following way:
$date = DateTime::createFromFormat('j/n/Y', '9/6/2013');
This sample is not just obvious for this situation but it is even useful in many other places too for eg getting info from an mathematician entered quadratic equation in your site text box without regex
or driving licence number can be properly handled and stored in a database formatting it using scanf for eg scanf($input, '%s-%s-%d/%d); etc
it depends on your need so you have to know about sscanf, scanf, printf, sprintf, vsprintf
<?php
$date = '9/6/2013';
list($month,$day,$year) = sscanf($date, '%d/%d/%d');
$date = sprintf('%d-%02d-%02d',$year,$month,$day);
echo $date;
?>

PHP convert date format dd/mm/yyyy => yyyy-mm-dd [duplicate]

This question already has answers here:
PHP Date String Format [duplicate]
(2 answers)
Closed 3 years ago.
I am trying to convert a date from dd/mm/yyyy => yyyy-mm-dd. I have using the mktime() function and other functions but I cannot seem to make it work. I have managed to explode the original date using '/' as the delimiter but I have no success changing the format and swapping the '/' with a '-'.
Any help will be greatly appreciated.
Dates in the m/d/y or d-m-y formats are disambiguated by looking
at the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y
format is assumed. Check more here.
Use the default date function.
$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
EDIT I just tested it, and somehow, PHP doesn't work well with dd/mm/yyyy format. Here's another solution.
$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));
Try Using DateTime::createFromFormat
$date = DateTime::createFromFormat('d/m/Y', "24/04/2012");
echo $date->format('Y-m-d');
Output
2012-04-24
EDIT:
If the date is 5/4/2010 (both D/M/YYYY or DD/MM/YYYY), this below method is used to convert 5/4/2010 to 2010-4-5 (both YYYY-MM-DD or YYYY-M-D) format.
$old_date = explode('/', '5/4/2010');
$new_data = $old_date[2].'-'.$old_date[1].'-'.$old_date[0];
OUTPUT:
2010-4-5
Here's another solution not using date(). not so smart:)
$var = '20/04/2012';
echo implode("-", array_reverse(explode("/", $var)));
Do this:
date('Y-m-d', strtotime('dd/mm/yyyy'));
But make sure 'dd/mm/yyyy' is the actual date.
I can see great answers, so there's no need to repeat here, so I'd like to offer some advice:
I would recommend using a Unix Timestamp integer instead of a human-readable date format to handle time internally, then use PHP's date() function to convert the timestamp value into a human-readable date format for user display. Here's a crude example of how it should be done:
// Get unix timestamp in seconds
$current_time = date();
// Or if you need millisecond precision
// Get unix timestamp in milliseconds
$current_time = microtime(true);
Then use $current_time as needed in your app (store, add or subtract, etc), then when you need to display the date value it to your users, you can use date() to specify your desired date format:
// Display a human-readable date format
echo date('d-m-Y', $current_time);
This way you'll avoid much headache dealing with date formats, conversions and timezones, as your dates will be in a standardized format (Unix Timestamp) that is compact, timezone-independent (always in UTC) and widely supported in programming languages and databases.

Categories