save Date Number in PHP variable - php

This question goes along with another one of my post that I already accepted
How do I get the "date number" in php
2010-08-24 20:00:00.000
I want to assign the current date number to a variable $current_date_num so I can use it in my query to compare what is already in the database.
$query ="SELECT * FROM Reservations WHERE [Room_ID] = '$field' AND [Meeting Start] > '$current_date_num' ORDER BY [Meeting Start] asc ";

If you are only using it in the database, consider NOW

To get the current date number in the format above you would do:
echo date("Y-m-d H:i:s.u")

well, you can get the date number like this.. ex. from php.net http://php.net/manual/en/function.date.php.
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
?>

$current_date_num = date("Y-m-d H:i:s.u", time());
refer to http://php.net/manual/en/function.date.php

If you are trying to specify it in the page from user side
use it this way
strtotime($time1)

Related

php format date into mysql format for insert [duplicate]

This question already has answers here:
PHP date() format when inserting into datetime in MySQL
(14 answers)
Closed 1 year ago.
I have a php variable which represents date (is from a date picker) and is in this format "DD.MM.YYYY" example today "03.03.2021".
I see mysql date supported format is "YYYY-MM-DD". If i just try to insert my variable in mysql I get "0000-00-00".
How can I convert my variable to mysql format to be able to insert it in the table?
Thank you
You can try this
$str = "03.03.2021";
$date = DateTime::createFromFormat("d.m.Y", $str);
echo date_format($date,"Y-m-d");
You can always convert date like:
date('Y-m-d', strtotime('03.03.2021'))
print_r results
print_r(date('Y-m-d', strtotime('03.03.2021')));
2021-03-03
print_r(date('d/m/Y', strtotime('03.03.2021')));
03/03/2021
And so on...You can use date formats like:
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
From php.net
Btw, I'd strongly encourage you to store the date like a timestamp instead.

Convert a date of YYYY-mm-dd format to dd-mm format using PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a date: 2015-06-24
I want to display it as 24 June
I have written this code.
$evnt_start_date = date('F m', strtotime($evnt_details['events_date']));
It displays the result like: June 06
What am I doing wrong?
m is number of month. Use d in reverse order:
$evnt_start_date = date('d F', strtotime($evnt_details['events_date']));
read more about date()
<?php
echo date('d F', strtotime($evnt_details['events_date']));
?>
This will output in this format DD-MM
date() Formatting Methods...
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>
Below code will give you 24 June.
date("d F",strtotime($evnt_details['events_date']));
Many of the answers above will work just fine for you, but I'd like to suggest a really awesome way to work with Dates and Time in PHP. It's DateTime and their related classes; to answer your question:
$dateString = '2015-06-24';
$date = new \DateTime($dateString);
$evnt_start_date = $date->format('d F');
See the following links:
http://php.net/manual/en/class.datetime.php - DateTime
Some extra reading that will help both now and in the future:
http://www.phptherightway.com/#date_and_time - PHP the right way
Use j for not leading 0 and use d for leading zero.
Not leading Zero:
$evnt_details['events_date'] = '2015-06-24';
echo $evnt_start_date = date('j F', strtotime($evnt_details['events_date']));//24 June
Leading Zero:
$evnt_details['events_date'] = '2015-06-24';
echo $evnt_start_date = date('d F', strtotime($evnt_details['events_date']));//24 June

PHP Date Format Confusion

I have this date in my database 11/06/2013 12:00
This is my code and the format I would like it in
$dateformatstart = date_format(date_create($row['datestart']), 'D j M y H:i');
Although it comes back as
Wed 6 Nov 13 12:00
Think the month is the day and day is the month, I don't know why
Thanks
Maybe this helps:
$date = DateTime::createFromFormat('d/m/Y H:i', $row['datestart']);
$dateformatstart = date_format($date, 'D j M y H:i');
Have you even checked out the php manual before asking this question?
Well, Here are some clever ways on how to format dates in PHP
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y/m/d H:i"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
If you are trying to output a data format like this :
11/06/2013 12:00
then better, Use
$today = date("d/m/Y H:i");

How to change the language of ".$upcoming date." in reminder emails to site users

I urgently need to know how I can make dates appear in Spanish instead of in English in reminder emails sent to users via a cron task. With the current (".$upcoming_date."), the date shown in the emails is like this: Saturday, 26 November, 2011 21:19, and I would need this to change into the corresponding Spanish version or into something like 26/11/2011 21:19.
You will maybe need this function to convert the time if it is store somewhere. Good luck.
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
You don't. You change the value of the $format argument you pass to strftime().
strftime(_($someformat), $sometime)
For spanish dates ,You can do:
echo strftime("%A %e de %B del %Y - %H:%M:%S", mktime (gmdate("H"), gmdate("i"), gmdate("s")-16200, gmdate("n"), gmdate("j"), gmdate("Y")));
Hope it helps

convert unix timestmp to date

how to I convert a unix timestamp 1280214000 to human readable date?
you can also use the date function
http://us.php.net/manual/en/function.date.php
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
?>
Just pass in your unix timestamp as the second parameter.
You can use strftime. E.g.:
strftime("%x", 1280214000);
See the documentation for all the formatting options.
You can use date:
date('Y-m-d H:i:s', $timestamp);
//Monday 8th of August 2005 03:12:46 PM
echo(date("l jS \of F Y h:i:s A", "1280214000"));
You can use the date function.
http://php.net/manual/en/function.date.php

Categories