Get a timestamp from a day,year, and month [duplicate] - php

This question already has answers here:
How to convert date to timestamp in PHP?
(22 answers)
Closed 9 years ago.
I have the following string: 2013-03-22
I need to convert that to a UNIX Timestamp.
How do I do this using PHP?

Use the strtotime function of PHP.
Like
$unixstamp = strtotime("2013-03-22");

With DateTime class
$dateTime = DateTime::createFromFormat('Y-m-d', '2013-03-22');
$dateTime->setTime(0, 0, 0);
echo $dateTime->getTimestamp();

Simple:
echo $time = strtotime("2013-03-22");

Using
strtotime("2013-03-22")
It will convert the date sting into timestamp value.

Related

How to get Custom date format from timestamp php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a time stamp like this code below.
$time=time();
Please Explain how can i make a custom date from this.
See the documentation here: http://php.net/manual/en/function.date.php
string date ( string $format [, int $timestamp = time() ] )
As an example:
echo date('Y-m-d', $time);
Please use php builtin date function like this.
$time=time();
$custom_date=date('Y-m-d',$time);
echo $custom_date;
i hope you understand.

PHP Date conversion to unix-timestamp [duplicate]

This question already has answers here:
How to convert date to timestamp in PHP?
(22 answers)
Closed 8 years ago.
How do yo convert this 2014-11-03 17:01:37 into Unix Timestamp?
Is there a method that I can use.
You can create a DateTime object:
$time = new DateTime("2014-11-03 17:01:37");
$time->getTimestamp();
use - strtotime()
$date = "2014-11-03 17:01:37";
echo strtotime($date);
strtotime('2014-11-03 17:01:37')
PHP has a function strtotime for that purpose.
echo strtotime("2014-11-03 17:01:37");

Get current date as mm/dd/yyyy in php using strftime() because setlocale() is used [duplicate]

This question already has answers here:
How do I create a variable in PHP of today's date of MM/DD/YYYY format?
(6 answers)
Closed 8 years ago.
I am using:
strftime("%m/%d/%Y", date())
But it gives:
12/31/1969
I want current date like mm/dd/yyyy
Edit:
Can I get current date using strftime? because the code has setlocale() used
If you want such format:
echo strftime("%m/%d/%Y"); // 06/07/2014
or the date Function
echo date('m/d/Y'); // 06/07/2014
or the DateTime Class
$date = new DateTime();
echo $date->format('m/d/Y'); // 06/07/2014
You can use date like this
$data = date('m/d/Y');
You were attempting to use mysql-style date formatting. As you observed, that didn't work.

Convert date in format MM/DD/YYYY to MySQL date [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
PHP mysql insert date format
(11 answers)
Closed 9 years ago.
I have a PHP function that gets passed a date in the format MM/DD/YYYY
I need to then convert this so that it can be added to a MySQL field that is of type date
How would I go about doing this in PHP?
$newvalue = date('Y-m-d', strtotime($originalvalue));
MySQL displays the DATE type as 'YYYY-MM-DD', so you could do something like:
date("Y-m-d",strtotime("10/18/2013"));
My variant:
$mysql_date = date('Y-m-d', strtotime(str_replace('/','-', $value)));
$date = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3-$1-$2', $date)

Convert unix timestamp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert Unix timestamp to hhmmss?
Here is unix timestamp of today
1306702800
How to convert it to look like 30-05-2011 using php ?
You can use the date function to convert (and arbitrarily re-format) timestamps as such:
echo date('d-m-Y', 1306702800);
Additionally, you can get the current timestamp via the time function, so you could output the current date via: echo date('d-m-Y', time());.
Use PHP's date functions: http://nl3.php.net/manual/en/function.date.php
See date
echo date('d-m-Y', '%your-time-stamp%');

Categories