Convert ordered string to mysql Datetime - php

I have an API that returns a date string like this : 190416102906
Which means : 19 April 2016 at 10:29:06.
I want to store it in my Mysql Datetime but I don't know how to format it. It need to go from 190416102906 to 2016-04-19 10:29:06.
I've tried to use something like this but I probably didn't understand how it works :
$date_reponse = '190416102906';
$date = DateTime::createFromFormat("dmYHis",$date_reponse);
$newDate = date_format($date, 'Y-m-d H:i:s');

Because your format was incorrect : "dmYHis"
Y = Year format with 4 digit.
use y year with 2 digit (dmyHis)
$date = DateTime::createFromFormat("dmyHis",$date_reponse);

You just got the format the wrong way round and you need a lower case y for a 2 char year
$date_reponse = '190416102906';
$date = DateTime::createFromFormat("dmyHis",$date_reponse);
$newDate = date_format($date, 'Y-m-d H:i:s');

Related

date conversion in CodeIgniter / PHP does not gives output

I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>

Changed position from day and month converting strings to date

I am working on following issue in PHP.
I have these variables:
$fecha = "01-07-2017";
$hora = "11:30";
Now I want to create a date variable with format Y-m-d H:i.
For that I am using following code:
$newDate = date("Y-m-d H:i", strtotime($fecha.' '.$hora));
But I am getting the output:
echo $newDate = 2017-01-07 11:30
I need it to be: 2017-07-01 11:30
What am I doing wrong?
Use the DateTime API to parse your string(s) initially
$dt = DateTime::createFromFormat('d-m-Y H:i', $fecha.' '.$hora);
$newDt = $dt->format('Y-m-d H:i');
Demo ~ https://eval.in/827662
Change your code to:
$newDate = date("Y-d-m H:i", strtotime($fecha.' '.$hora));
Notice that I just switched around 'h' & 'd' in the date function.

Date Format from common string

I have a date format but still on String like this :
$date = '24-01-2017' # (dd-mm-yy)
I want to display them into datetime in Indonesian format
Selasa, 24-01-2017
In english
Tuesday, 24-01-2017
So, I created like this :
$date_format = DateTime::createFromFormat(' j-M-Y', $date);
echo $date_format->date('d-M-Y')
Not works
You should be using the format() function.
$now = new DateTime();
echo $now->format('Y-m-d');
// or
$now = DateTime::createFromFormat('d-m-Y', $date);
echo $now->format('Y-m-d');
Change the format to whatever you please.
To get the day in your own language:
$days = array(
'0' => 'Mon day',
'1' => 'Tues - Day',
// ... etc
);
You can now do:
$day = $now->format('n');
echo $days[$day].', '.$now->format('d-m-Y');
From a website that I found by doing a search for 'Date Time in Indonesian format, php'
PHP : Displaying the date and time in indonesian language
By this time I hope you can create your own date and time using the timestamp
of the current date or your own timestamp.
I use Indonesia language and I want to
display the date in Indonesian language. How can I do it? That's a perfect
question and asked by most beginners.
Let's create an associative array of date and month in our own language.
$days = Array ("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu");
$months = Array (1=>"Januari", 2=>"Pebruari", 3=>"Maret", 4=>"April",
5=>"Mei", 6=>"Juni", 7=>"Juli", 8=>"Agustus", 9=>"September",
10=>"Oktober", 11=>"Nopember", 12=>"Desember");
After we create our own language of date and months, now it's time to display it.
print $days[date("w")]; // display name of day with our own language
print $months[date("n")];
From php.net :
F and M : A textual representation of a month, such as January or Sept January through December or Jan through Dec
m and n : Numeric representation of a month, with or without leading zeros 01 through 12 or 1 through 12
You have to use "m" and not "M"
BTW, the method is not "date" but "format". ;)
<?php
$date = '24-01-2017';
$date_format = DateTime::createFromFormat('j-m-Y', $date);
echo $date_format->format('D d-m-Y');
?>
This outputs :
Tue 24-01-2017

Php strtotime not showing the valid date

i have a mysql table with column type DATETIME, i want it to display like 19 Aug, 2013.
so i have tried with
echo $date = date('Y-m-d H:i:s');
echo '<br/>';
echo date('y M ,Y',strtotime($date));
The output im getting is
2013-08-19 22:47:12
13 Aug ,2013
The i tried with
$datetime = DateTime::createFromFormat('Y-m-d', '2013-08-19');
echo $datetime->format('yM,Y');
But it also outputs the wrong date 13Aug,2013
Any one have faced the same kind of issue.
y is two-digit year, you want d, which is day. See also the documentation.
You used y twice:
echo $date = date('Y-m-d H:i:s');
echo '<br/>';
echo date('d M ,Y',strtotime($date));
To me it looks like it's doing exactly what it should - but the format specifier you are passing to date() and dateTime->format() looks strange - 'y' returns the year as 2 digits, 'Y' returns a 4 digit year. Did you mean that you wanted the day of the month at the start of he output?
Try 'd' or 'j' in place of 'y'.

String date current date/time?

I am using $date = date("D M d, Y G:i");.
When I echo $date, it shows the correct date/time. Now I need this as an string.
I have tried string($date); but nothing happens here. And
$today = strtotime($date);
here I get weird numbers..
I need a string so I can put $today in a message.
What is the correct method for this?
The date() function already returns a string.
Doing this :
$date = date("D M d, Y G:i");
You'll have the current date in the $date variable, as a string -- no need for any additional operation.
If you like working with objects you can do this:
$date = new \DateTime('now');
echo $date->format('D M d, Y G:i');
Your $date variable is a string, there's no need for any conversion.
You can have a look at the documentation: http://ch.php.net/manual/en/function.date.php. The return value of the date() function is string.
The strange numbers you see when you call strtotime() is the Unix timestamp which represents the number of seconds elapsed since January 1 1970 00:00:00 UTC.
You're already getting a string. $date can be used like any string now.
strtotime() actually gives you the number of seconds in time like unix
$date = 'Today is '.date("D M d, Y G:i", time());
echo $date;
With regards to:
$today = strtotime($date);
Those numbers are the current timestamp (the number of seconds since January 1st 1970).
You can use this as a second parameter in the date function to change the date to whatever you want.
$newDate = date("D M d, Y G:i", $timeStamp);

Categories